Java Set 7 (30 mcqs)

1. Which of these method of class String is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned

2. What will s2 contain after following lines of code?
String s1 = “one”;
String s2 = s1.concat(“two”)
a) one
b) two
c) onetwo
d) twoone

3. Which of these method of class String is used to remove leading and trailing whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()

4. What is the value returned by function compareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) None of the mentioned

5. Which of the following statement is correct?
a) replace() method replaces all occurrences of one character in invoking string with another character.
b) replace() method replaces only first occurances of a character in invoking string with another character.
c) replace() method replaces all the characters in invoking string with another character.
d) replace() replace() method replaces last occurrence of a character in invoking string with another character.

6. What is the output of this program?
1.class output {
2.    public static void main(String args[])
3.    {
4.       String c = ”  Hello World  “;
5.       String s = c.trim();
6.       System.out.println(“\””+s+”\””);
7.    }
8.}
a) ” Hello World ”
b) ” Hello World ”
c) “Hello World”
d) Hello world

7. What is the output of this program?
1.class output {
2.    public static void main(String args[])
3.    {
4.       String s1 = “one”;
5.       String s2 = s1 + ” two”;
6.       System.out.println(s2);
7.    }
8.}
a) one
b) two
c) one two
d) compilation error

8. What is the output of this program?
1.class output {
2.    public static void main(String args[])
3.    {
4.       String s1 = “Hello”;
5.       String s2 = s1.replace(‘l’,’w’);
6.       System.out.println(s2);
7.    }
8.}
a) hello
b) helwo
c) hewlo
d) hewwo

9. What is the output of this program?
1.class output {
2.    public static void main(String args[])
3.    {
4.       String s1 = “Hello World”;
5.       String s2 = s1.substring(0 , 4);
6.       System.out.println(s2);
7.    }
8.       }
a) Hell
b) Hello
c) Worl
d) World

10. What is the output of this program?
1.class output {
2.    public static void main(String args[])
3.    {             String s = “Hello World”;
4.         int i = s.indexOf(‘o’);
5.         int j = s.lastIndexOf(‘l’);
6.         System.out.print(i + ” ” + j);
7.
8.    }
9.       }
a) 4 8
b) 5 9
c) 4 9
d) 5 8

11. What is the use of try & catch?
a) It allows us to manually handle the exception.
b) It allows to fix errors.
c) It prevents automatic terminating of the program in cases when an exception occurs.
d) All of the mentioned.

12. Which of these keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check

13. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check

14. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check

15. Which of these statements is incorrect?
a) try block need not to be followed by catch block.
b) try block can be followed by finally block instead of catch block.
c) try can be followed by both catch and finally block.
d) try need not to be followed by anything.

16. What is the output of this program?
1.class Output {
2.    public static void main(String args[]) {
3.       try {
4.           int a = 0;
5.           int b = 5;
6.           int c = b / a;
7.           System.out.print(“Hello”);
8.       }
9.       catch(Exception e) {
10.           System.out.print(“World”);
11.       }
12.    }
13.}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error

17. What is the output of this program?
1.class Output {
2.    public static void main(String args[]) {
3.       try {
4.           int a = 0;
5.           int b = 5;
6.           int c = a / b;
7.           System.out.print(“Hello”);
8.       }
9.       catch(Exception e) {
10.           System.out.print(“World”);
11.       }
12.    }
13.}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error

18. What is the output of this program?
1.class Output {
2.    public static void main(String args[]) {
3.       try {
4.           int a = 0;
5.           int b = 5;
6.           int c = b / a;
7.           System.out.print(“Hello”);
8.       }
9.    }
10.}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error

19. What is the output of this program?
1.class Output {
2.    public static void main(String args[]) {
3.       try {
4.           int a = 0;
5.           int b = 5;
6.           int c = a / b;
7.           System.out.print(“Hello”);
8.       }
9.       finally {
10.           System.out.print(“World”);
11.       }
12.    }
13.}
a) Hello
b) World
c) HelloWOrld
d) Compilation Error

20. What is the output of this program?
1.class Output {
2.    public static void main(String args[]) {
3.       try {
4.           int a = 0;
5.           int b = 5;
6.           int c = b / a;
7.           System.out.print(“Hello”);
8.       }
9.       catch(Exception e) {
10.           System.out.print(“World”);
11.       }
12.       finally {
13.           System.out.print(“World”);
14.       }
15.    }
16.}
a) Hello
b) World
c) HelloWOrld
d) WorldWorld

21. Which of these package is used for graphical user interface?
a) java.applet
b) java.awt
c) java.awt.image
d) java.io

22. Which of these package is used for analyzing code during run-time?
a) java.applet
b) java.awt
c) java.io
d) java.lang.reflect

23. Which of these package is used for handling security related issues in a program?
a) java.security
b) java.lang.security
c) java.awt.image
d) java.io.security

24. Which of these class allows us to get real time data about private and protected member of a class?
a) java.io
b) GetInformation
c) ReflectPermission
d) MembersPermission

25. Which of these package is used for invoking a method remotely?
a) java.rmi
b) java.awt
c) java.util
d) java.applet

26. Which of these package is used for all the text related modifications?
a) java.text
b) java.awt
c) java.lang.text
d) java.text.mofify

27. What is the output of this program?
1.import java.lang.reflect.*;
2.class Additional_packages {
3.     public static void main(String args[]) {
4.     try {
5.         Class c = Class.forName(“java.awt.Dimension”);
6.             Constructor constructors[] = c.getConstructors();
7.             for (int i = 0; i < constructors.length; i++)
8.         System.out.println(constructors[i]);
9.             }
10.     catch (Exception e){
11.         System.out.print(“Exception”);
12.         }
13.    }
14.}
a) Program prints all the constructors of ‘java.awt.Dimension’ package.
b) Program prints all the possible constructors of class ‘Class’.
c) Program prints “Exception”
d) Runtime Error

28. What is the output of this program?
1.import java.lang.reflect.*;
2.class Additional_packages {
3.     public static void main(String args[]) {
4.     try {
5.         Class c = Class.forName(“java.awt.Dimension”);
6.             Field fields[] = c.getFields();
7.             for (int i = 0; i < fields.length; i++)
8.         System.out.println(fields[i]);
9.             }
10.     catch (Exception e){
11.         System.out.print(“Exception”);
12.         }
13.    }
14.}
a) Program prints all the constructors of ‘java.awt.Dimension’ package.
b) Program prints all the methods of ‘java.awt.Dimension’ package.
c) Program prints all the data members of ‘java.awt.Dimension’ package.
d) program prints all the methods and data member of ‘java.awt.Dimension’ package.

29. What is the length of the application box made by this program?
1.import java.awt.*;
2.import java.applet.*;
3.public class myapplet extends Applet {
4.    Graphic g;
5.    g.drawString(“A Simple Applet”,20,20);
6.}
a) 20
b) Default value
c) Compilation Error
d) Runtime Error

30. What is the output of this program?
1.import java.lang.reflect.*;
2.class Additional_packages {
3.     public static void main(String args[]) {
4.     try {
5.         Class c = Class.forName(“java.awt.Dimension”);
6.             Method methods[] = c.getMethods();
7.             for (int i = 0; i < methods.length; i++)
8.         System.out.println(methods[i]);
9.             }
10.     catch (Exception e){
11.         System.out.print(“Exception”);
12.         }
13.    }
14.}
a) Program prints all the constructors of ‘java.awt.Dimension’ package.
b) Program prints all the methods of ‘java.awt.Dimension’ package.
c) Program prints all the data members of ‘java.awt.Dimension’ package.
d) program prints all the methods and data member of ‘java.awt.Dimension’ package.

Answers

1-a 2-c 3-b 4-b 5-a
6-c 7-c 8-d 9-a 10-c
11-d 12-a 13-b 14-c 15-d
16-b 17-a 18-d 19-c 20-d
21-b 22-d 23-a 24-c 25-a
26-a 27-a 28-c 29-c 30-b

Sharing is caring!

Leave a Comment

Your email address will not be published. Required fields are marked *

shares