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?
-
class Relational_operator
-
{
-
public static void main(String args[])
-
{
-
int var1 = 5;
-
int var2 = 6;
-
System.out.print(var1 > var2);
-
}
-
}
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?
-
class bool_operator
-
{
-
public static void main(String args[])
-
{
-
boolean a = true;
-
boolean b = !true;
-
boolean c = a | b;
-
boolean d = a & b;
-
boolean e = d ? b : c;
-
System.out.println(d + " " + e);
-
}
-
}
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?
-
class ternary_operator
-
{
-
public static void main(String args[])
-
{
-
int x = 3;
-
int y = ~ x;
-
int z;
-
z = x > y ? x : y;
-
System.out.print(z);
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int x , y = 1;
-
x = 10;
-
if (x != 10 && x / 0 == 0)
-
System.out.println(y);
-
else
-
System.out.println(++y);
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
boolean a = true;
-
boolean b = false;
-
boolean c = a ^ b;
-
System.out.println(!c);
-
}
-
}
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?
-
class A
-
{
-
int x;
-
int y;
-
void display()
-
{
-
System.out.print(x + " " + y);
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
A obj1 = new A();
-
A obj2 = new A();
-
obj1.x = 1;
-
obj1.y = 2;
-
obj2 = obj1.clone();
-
obj1.display();
-
obj2.display();
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 3.14;
-
int y = (int) Math.abs(x);
-
System.out.print(y);
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 3.14;
-
int y = (int) Math.ceil(x);
-
System.out.print(y);
-
}
-
}
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?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
double x = 3.14;
-
int y = (int) Math.floor(x);
-
System.out.print(y);
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(obj.getSuperclass());
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(b.equals(a));
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(obj.isInstance(a));
-
}
-
}
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?
-
class X
-
{
-
int a;
-
double b;
-
}
-
class Y extends X
-
{
-
int c;
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
X a = new X();
-
Y b = new Y();
-
Class obj;
-
obj = b.getClass();
-
System.out.print(obj.isLocalClass());
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String c = " Hello World ";
-
String s = c.trim();
-
System.out.println("""+s+""");
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String s1 = "one";
-
String s2 = s1 + " two";
-
System.out.println(s2);
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String s1 = "Hello";
-
String s2 = s1.replace('l','w');
-
System.out.println(s2);
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{
-
String s1 = "Hello World";
-
String s2 = s1.substring(0 , 4);
-
System.out.println(s2);
-
}
-
}
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?
-
class output
-
{
-
public static void main(String args[])
-
{ String s = "Hello World";
-
int i = s.indexOf('o');
-
int j = s.lastIndexOf('l');
-
System.out.print(i + " " + j);
-
-
}
-
}
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?
-
class string_demo
-
{
-
public static void main(String args[])
-
{
-
String obj = "I" + "like" + "Java";
-
System.out.println(obj);
-
}
-
}
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?
-
class string_class
-
{
-
public static void main(String args[])
-
{
-
String obj = "I LIKE JAVA";
-
System.out.println(obj.charAt(3));
-
}
-
}
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?
-
class string_class
-
{
-
public static void main(String args[])
-
{
-
String obj = "I LIKE JAVA";
-
System.out.println(obj.length());
-
}
-
}
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?
-
class string_class
-
{
-
public static void main(String args[])
-
{
-
String obj = "hello";
-
String obj1 = "world";
-
String obj2 = obj;
-
obj2 = " world";
-
System.out.println(obj + " " + obj2);
-
}
-
}
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?
-
class string_class
-
{
-
public static void main(String args[])
-
{
-
String obj = "hello";
-
String obj1 = "world";
-
String obj2 = "hello";
-
System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
-
}
-
}
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