Java MCQ Number 01068

Java MCQ Set 1

1. What is the output of relational operators?
a) Integer
b) Boolean
c) Characters
d) Double

Answer

Answer: b [Reason:] None.

2. Which of these is returned by “greater than”, “less than” and “equal to” operators?
a) Integers
b) Floating – point numbers
c) Boolean
d) None of the mentioned

Answer

Answer:c [Reason:] All relational operators return a boolean value ie. true and false.

3. Which of the following operators can operate on a boolean variable?

   1. &&
   2. ==
   3. ?:
   4. +=

a) 3 & 2
b) 1 & 4
c) 1, 2 & 4
d) 1, 2 & 3

Answer

Answer: d [Reason:] Operator Short circuit AND, &&, equal to, == , ternary if-then-else, ?:, are boolean logical operators. += is an arithmetic operator it can operate only on numeric values.

4. Which of these operators can skip evaluating right hand operand?
a) !
b) |
c) &
d) &&

Answer

Answer: d [Reason:] Operator short circuit and, &&, and short circuit or, ||, skip evaluating right hand operand when output can be determined by left operand alone.

5. Which of these statement is correct?
a) true and false are numeric values 1 and 0
b) true and false are numeric values 0 and 1
c) true is any non zero value and false is 0
d) true and false are non numeric values

Answer

Answer: d [Reason:] True and false are keywords, they are non numeric values which do no relate to zero or non zero numbers. true and false are boolean values.

6. What is the output of this program?

  1.     class Relational_operator 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 5; 
  6.             int var2 = 6;
  7.             System.out.print(var1 > var2);
  8.         } 
  9.     }

a) 1
b) 0
c) true
d) false

Answer

Answer:d [Reason:] Operator > returns a boolean value. 5 is not greater than 6 therefore false is returned.
output:
$ javac Relational_operator.java
$ java Relational_operator
false

7. What is the output of this program?

  1.     class bool_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              boolean a = true;
  6.              boolean b = !true;
  7.              boolean c = a | b;
  8.  	     boolean d = a & b;
  9.              boolean e = d ? b : c;
  10.              System.out.println(d + " " + e);
  11.         } 
  12.     }

a) false false
b) true ture
c) true false
d) false true

Answer

Answer: d [Reason:] Operator | returns true if any one operand is true, thus ‘c = true | false’ is true. Operator & returns a true if both of the operand is true thus d is false. Ternary operator ?: assigns left of ‘:’ if condition is true and right hand of ‘:’ if condition is false. d is false thus e = d ? b : c , assigns c to e , e contains true.
output:
$ javac bool_operator.java
$ java bool_operator
false true

8. What is the output of this program?

  1.     class ternary_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              int x = 3;
  6.              int y = ~ x;
  7.              int z;
  8.              z = x > y ? x : y;
  9.              System.out.print(z);
  10.         } 
  11.     }

a) 0
b) 1
c) 3
d) -4

Answer

Answer:c [Reason:] None.
output:
$ javac ternary_operator.java
$ java ternary_operator
3

9. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x , y = 1;
  6.              x = 10;
  7.              if (x != 10 && x / 0 == 0)
  8.                  System.out.println(y);
  9.              else
  10.                  System.out.println(++y);
  11.         } 
  12.     }

a) 1
b) 2
c) Runtime error owing to division by zero in if condition
d) Unpredictable behavior of program

Answer

Answer: b [Reason:] Operator short circuit and, &&, skips evaluating right hand operand if left hand operand is false thus division by zero in if condition does not give an error.
output:
$ javac Output.java
$ java Output
2

10. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              boolean a = true;
  6.              boolean b = false;
  7.              boolean c = a ^ b;
  8.              System.out.println(!c);
  9.         } 
  10.     }

a) 0
b) 1
c) false
d) true

Answer

Answer: c [Reason:] None.
output:
$ javac Output.java
$ java Output
false

Java MCQ Set 2

1. Which of these class provides various types of rounding functions?
a) Math
b) Process
c) System
d) Object

Answer

Answer: a [Reason:] None.

2. Which of these method return a smallest whole number greater than or equal to variable X?
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)

Answer

Answer: a [Reason:] ceil(double X) returns the smallest whole number greater than or equal to variable X.

3. Which of these method return a largest whole number less than or equal to variable X?
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)

Answer

Answer: b [Reason:] double floor(double X) returns a largest whole number less than or equal to variable X.

4. Which of these method is a rounding function of Math class?
a) max()
b) min()
c) abs()
d) rint()

Answer

Answer: d [Reason:] rint() rounds up a variable to nearest integer.

5. Which of these class contains only floating point functions?
a) Math
b) Process
c) System
d) Object

Answer

Answer: a [Reason:] Math class contains all the floating point functions that are used for geometry, trigonometry, as well as several general purpose methods. Example : sin(), cos(), exp(), sqrt() etc.

6. Which of function return absolute value of a variable?
a) abs()
b) absolute()
c) absolutevariable()
d) none of the mentioned

Answer

Answer: a [Reason:] abs() returns the absolute value of a variable.

7. What is the output of this program?

  1.     class A 
  2.     {
  3.          int x;
  4.          int y;
  5.          void display() 
  6.          {
  7.               System.out.print(x + " " + y);
  8.          }
  9.     }
  10.     class Output 
  11.     {
  12.          public static void main(String args[]) 
  13.          {
  14.              A obj1 = new A();
  15.              A obj2 = new A();
  16.              obj1.x = 1;
  17.              obj1.y = 2;
  18.              obj2 = obj1.clone();
  19.              obj1.display();
  20.              obj2.display();
  21.          }
  22.     }

a) 1 2 0 0
b) 1 2 1 2
c) 0 0 0 0
d) System Dependent

Answer

Answer: b [Reason:] clone() method of object class is used to generate duplicate copy of the object on which it is called. Copy of obj1 is generated and stored in obj2.
Output:
$ javac Output.java
$ java Output
1 2 1 2

8. What is the output of this program?

  1.     class Output 
  2.     {
  3.          public static void main(String args[]) 
  4.          {
  5.              double x = 3.14;  
  6.              int y = (int) Math.abs(x);
  7.              System.out.print(y);
  8.          }
  9.     }

a) 0
b) 3
c) 3.0
d) 3.1

Answer

Answer: b [Reason:] None.
Output:
$ javac Output.java
$ java Output
3

9. What is the output of this program?

  1.     class Output 
  2.     {
  3.          public static void main(String args[]) 
  4.          {
  5.              double x = 3.14;  
  6.              int y = (int) Math.ceil(x);
  7.              System.out.print(y);
  8.          }
  9.     }

a) 0
b) 3
c) 3.0
d) 4

Answer

Answer: d [Reason:] ciel(double X) returns the smallest whole number greater than or equal to variable x.
Output:
$ javac Output.java
$ java Output
4

10. What is the output of this program?

  1.     class Output 
  2.     {
  3.          public static void main(String args[]) 
  4.          {
  5.              double x = 3.14;  
  6.              int y = (int) Math.floor(x);
  7.              System.out.print(y);
  8.          }
  9.     }

a) 0
b) 3
c) 3.0
d) 4

Answer

Answer: d [Reason:] double floor(double X) returns a largest whole number less than or equal to variable X. Here the smallest whole number less than 3.14 is 3.
Output:
$ javac Output.java
$ java Output
3

Java MCQ Set 3

1. Which of these classes encapsulate runtime enviroment?
a) Class
b) System
c) Runtime
d) ClassLoader

Answer

Answer: c [Reason:] None.

2. Which of the following exceptions is thrown by every method of Runtime class?
a) IOException
b) SystemException
c) SecurityException
d) RuntimeException

Answer

Answer: c [Reason:] Every method of Runtime class throws SecurityException.

3. Which of these methods returns the total number of bytes of memory available to the program?
a) getMemory()
b) TotalMemory()
c) SystemMemory()
d) getProcessMemory()

Answer

Answer: b [Reason:] TotalMemory() returns the total number of bytes available to the program.

4. Which of these class defines how the classes are loaded?
a) Class
b) System
c) Runtime
d) ClassLoader

Answer

Answer: d [Reason:] None.

5. Which of these methods return a class object given its name?
a) getClass()
b) findClass()
c) getSystemClass()
d) findSystemClass()

Answer

Answer: d [Reason:] findSystemClass() returns a class object given its name.

6. Which of these Exceptions is thrown by loadClass() method of ClassLoader class?
a) IOException
b) SystemException
c) ClassFormatError
d) ClassNotFoundException

Answer

Answer: d [Reason:] None.

7. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.getSuperclass());
  19.         }
  20.     }

a) X
b) Y
c) class X
d) class Y

Answer

Answer: c [Reason:] getSuperClass() returns the super class of an object. b is an object of class Y which extends class X , Hence Super class of b is X. therefore class X is printed.
Output:
$ javac Output.java
$ java Output
class X

8. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(b.equals(a));
  19.         }
  20.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
false

9. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.isInstance(a));
  19.         }
  20.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: d [Reason:] Although class Y extends class X but still a is not considered related to Y. hence isInstance() returns false.
Output:
$ javac Output.java
$ java Output
false

10. What is the output of this program?

  1.     class X 
  2.     {
  3.         int a;
  4.         double b;
  5.     }
  6.     class Y extends X 
  7.     {
  8. int c;
  9.     }
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             X a = new X();
  15.             Y b = new Y();
  16.             Class obj;
  17.             obj = b.getClass();
  18.             System.out.print(obj.isLocalClass());
  19.         }
  20.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
false

Java MCQ Set 4

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

Answer

Answer: a [Reason:] None.

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

Answer

Answer: c [Reason:] Two strings can be concatenated by using concat() method.

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

Answer

Answer: b [Reason:] None.

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

Answer

Answer: c [Reason:] if (s1 == s2) then 0, if(s1 > s2) > 0, if (s1 < s2) then < 0.

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

Answer

Answer: a [Reason:] replace() method replaces all occurrences of one character in invoking string with another character.

6. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String c = "  Hello World  ";
  6.            String s = c.trim();
  7.            System.out.println("""+s+""");
  8.         }
  9.     }

a) “”Hello World””
b) “”Hello World”
c) “Hello World”
d) Hello world

Answer

Answer: c [Reason:] trim() method is used to remove leading and trailing whitespaces in a string.
Output:
$ javac output.java
$ java output
“Hello World”

7. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String s1 = "one";
  6.            String s2 = s1 + " two";
  7.            System.out.println(s2);
  8.         }
  9.     }

a) one
b) two
c) one two
d) compilation error

Answer

Answer: c [Reason:] None.
Output:
$ javac output.java
$ java output
one two

8. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            String s1 = "Hello";
  6.            String s2 = s1.replace('l','w');
  7.            System.out.println(s2);
  8.         }
  9.     }

a) hello
b) helwo
c) hewlo
d) hewwo

Answer

Answer: d [Reason:] replace() method replaces all occurrences of one character in invoking string with another character. s1.replace(‘l’,’w’) replaces every occurrence of ‘l’ in hello by ‘w’, giving hewwo.
Output:
$ javac output.java
$ java output
hewwo

9. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.            String s1 = "Hello World";
  6.            String s2 = s1.substring(0 , 4);
  7.            System.out.println(s2);
  8.         }
  9.    }

a) Hell
b) Hello
c) Worl
d) World

Answer

Answer: a [Reason:] substring(0,4) returns the character from 0 th position to 3 rd position.
output:
$ javac output.java
$ java output
Hell

10. What is the output of this program?

  1.     class output 
  2.     {
  3.         public static void main(String args[])
  4.         {             String s = "Hello World";
  5.              int i = s.indexOf('o');
  6.              int j = s.lastIndexOf('l');
  7.              System.out.print(i + " " + j);
  8.  
  9.         }
  10.    }

a) 4 8
b) 5 9
c) 4 9
d) 5 8

Answer

Answer: c [Reason:] indexOf() method returns the index of first occurrence of the character where as lastIndexOf() returns the index of last occurrence of the character.
output:
$ javac output.java
$ java output
4 9

Java MCQ Set 5

1. String in Java is a?
a) class
b) object
c) variable
d) character array

Answer

Answer: a [Reason:] None.

2. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()

Answer

Answer: d [Reason:] None.

3. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) super
c) this
d) none of the mentioned

Answer

Answer: b [Reason:] Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.

4. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()

Answer

Answer: d [Reason:] None.

5. Which of the following statements are incorrect?
a) String is a class
b) Strings in java are mutable
c) Every string is an object of class String
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered

Answer

Answer: b [Reason:] Strings in Java are immutable that is they can not be modified.

6. What is the output of this program?

  1.     class string_demo 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "I" + "like" + "Java";   
  6.             System.out.println(obj);     
  7.         }
  8.    }

a) I
b) like
c) Java
d) IlikeJava

Answer

Answer: d [Reason:] Java defines an operator +, it is used to concatenate strings.
output:
$ javac string_demo.java
$ java string_demo
IlikeJava

7. What is the output of this program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "I LIKE JAVA";   
  6.             System.out.println(obj.charAt(3));
  7.         } 
  8.     }

a) I
b) L
c) K
d) E

Answer

Answer: a [Reason:] charAt() is a method of class String which gives the character specified by the index. obj.charAt(3) gives 4th character i:e I.
output:
$ javac string_class.java
$ java string_class
I

8. What is the output of this program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "I LIKE JAVA";   
  6.             System.out.println(obj.length());
  7.         }
  8.     }

a) 9
b) 10
c) 11
d) 12

Answer

Answer: c [Reason:] None.
output:
$ javac string_class.java
$ java string_class
11

9. What is the output of this program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "hello";
  6.             String obj1 = "world";   
  7.             String obj2 = obj;
  8.             obj2 = " world";
  9.             System.out.println(obj + " " + obj2);
  10.         }
  11.     }

a) hello hello
b) world world
c) hello world
d) world hello

Answer

Answer: c [Reason:] None.
output:
$ javac string_class.java
$ java string_class
hello world

10. What is the output of this program?

  1.     class string_class 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String obj = "hello";
  6.             String obj1 = "world";   
  7.             String obj2 = "hello";
  8.             System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
  9.         }
  10.     }

a) false false
b) true true
c) true false
d) false true

Answer

Answer: d [Reason:] equals() is method of class String, it is used to check equality of two String objects, if they are equal, true is retuned else false.
output:
$ javac string_class.java
$ java string_class
false true

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.