100+ Java Programming MCQs
Java Questions & Answers – Arithmetic Operators
1. Which of the following can be operands of arithmetic operators?
a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters
Answer: d
2. Modulus operator, %, can be applied to which of these?
a) Integers
b) Floating – point numbers
c) Both Integers and floating – point numbers
d) None of the mentioned
Answer: c
3. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
a) 1, 2 & 3
b) 1 & 4
c) 1, 2, 3 & 4
d) 3 & 2
Answer: c
4. Decrement operator, −−, decreases the value of variable by what number?
a) 1
b) 2
c) 3
d) 4
Answer: a
5. Which of these statements are incorrect?
a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms
b) Assignment operators run faster than their equivalent long forms
c) Assignment operators can be used only with numeric and character data type
d) None of the mentioned
Answer: d
6. What will be the output of the following Java program?
class increment
{
public static void main(String args[])
{
double var1 = 1 + 5;
double var2 = var1 / 4;
int var3 = 1 + 5;
int var4 = var3 / 4;
System.out.print(var2 + " " + var4);
}
}
a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0
Answer: c
7. What will be the output of the following Java program?
class Modulus
{
public static void main(String args[])
{
double a = 25.64;
int b = 25;
a = a % 10;
b = b % 10;
System.out.println(a + " " + b);
}
}
a) 5.640000000000001 5
b) 5.640000000000001 5.0
c) 5 5
d) 5 5.640000000000001
Answer: a
8. What will be the output of the following Java program?
class increment
{
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
a) 25
b) 24
c) 32
d) 33
Answer: c
9. Can 8 byte long data type be automatically type cast to 4 byte float data type?
a) True
b) False
Answer: a
10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
}
}
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
Answer: d
Java Questions & Answers – Bitwise Operators
1. Which of these is not a bitwise operator?
a) &
b) &=
c) |=
d) <=
Answer: d
2. Which operator is used to invert all the digits in a binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^
Answer: a
3. On applying Left shift operator, <<, on integer bits are lost one they are shifted past which position bit?
a) 1
b) 32
c) 33
d) 31
Answer: d
4. Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=
Answer: b
5. Which of these statements are incorrect?
a) The left shift operator, <<, shifts all of the bits in a value to the left specified number of times
b) The right shift operator, >>, shifts all of the bits in a value to the right specified number of times
c) The left shift operator can be used as an alternative to multiplying by 2
d) The right shift operator automatically fills the higher order bits with 0
Answer: d
6. What will be the output of the following Java program?
class bitwise_operator
{
public static void main(String args[])
{
int var1 = 42;
int var2 = ~var1;
System.out.print(var1 + " " + var2);
}
}
a) 42 42
b) 43 43
c) 42 -43
d) 42 43
Answer: c
7. What will be the output of the following Java program?
class bitwise_operator
{
public static void main(String args[])
{
int a = 3;
int b = 6;
int c = a | b;
int d = a & b;
System.out.println(c + " " + d);
}
}
a) 7 2
b) 7 7
c) 7 5
d) 5 2
Answer: a
8. What will be the output of the following Java program?
class leftshift_operator
{
public static void main(String args[])
{
byte x = 64;
int i;
byte y;
i = x << 2;
y = (byte) (x << 2)
System.out.print(i + " " + y);
}
}
a) 0 64
b) 64 0
c) 0 256
d) 256 0
Answer: d
9. What will be the output of the following Java program?
class rightshift_operator
{
public static void main(String args[])
{
int x;
x = 10;
x = x >> 1;
System.out.println(x);
}
}
a) 10
b) 5
c) 2
d) 20
Answer: b
10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println(a + " " + b + " " + c);
}
}
a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
Answer: a
Java Questions & Answers – Relational Operators and Boolean Logic Operators
1. What is the output of relational operators?
a) Integer
b) Boolean
c) Characters
d) Double
Answer: b
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: c
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: d
4. Which of these operators can skip evaluating right hand operand?
a) !
b) |
c) &
d) &&
Answer: d
5. Which of these statements 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: d
6. What will be the output of the following Java code?
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: d
7. What will be the output of the following Java code?
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: d
8. What will be the output of the following Java code?
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: c
9. What will be the output of the following Java code?
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: b
10. What will be the output of the following Java code?
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: c
Java Questions & Answers – Assignment Operators and Operator Precedence
1. Which of these have highest precedence?
a) ()
b) ++
c) *
d) >>
Answer: a
2. What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
Answer: c
3. What is the value stored in x in the following lines of Java code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
a) 0
b) 1
c) 9
d) 8
Answer: d
4. What is the order of precedence (highest to lowest) of following operators?
1. &
2. ^
3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
Answer: a
5. Which of these statements are incorrect?
a) Equal to operator has least precedence
b) Brackets () have highest precedence
c) Division operator, /, has higher precedence than multiplication operator
d) Addition operator, +, and subtraction operator have equal precedence
Answer: c
6. What will be the output of the following Java code?
class operators
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
int var3;
var3 = ++ var2 * var1 / var2 + var2;
System.out.print(var3);
}
}
a) 10
b) 11
c) 12
d) 56
Answer: c
7. What will be the output of the following Java code?
class operators
{
public static void main(String args[])
{
int x = 8;
System.out.println(++x * 3 + " " + x);
}
}
a) 24 8
b) 24 9
c) 27 8
d) 27 9
Answer: d
8. What will be the output of the following Java code?
class Output
{
public static void main(String args[])
{
int x=y=z=20;
}
}
a) compile and runs fine
b) 20
c) run time error
d) compile time error
Answer: d
9. Which of these lines of Java code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
a) 1 will give better performance as it has no parentheses
b) 2 will give better performance as it has parentheses
c) Both 1 & 2 will give equal performance
d) Dependent on the computer system
Answer: c
10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a,b,c,d;
a=b=c=d=20
a+=b-=c*=d/=20
System.out.println(a+" "+b+" "+c+" "+d);
}
}
a) compile time error
b) runtime error
c) a=20 b=0 c=20 d=1
d) none of the mentioned
Answer: c
Java Questions & Answers – Control Statements – 1
1. Which of these selection statements test only for equality?
a) if
b) switch
c) if & switch
d) none of the mentioned
Answer: b
2. Which of these are selection statements in Java?
a) if()
b) for()
c) continue
d) break
Answer: a
3. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
a) do-while
b) while
c) for
d) none of the mentioned
Answer: a
4. Which of these jump statements can skip processing the remainder of the code in its body for a particular iteration?
a) break
b) return
c) exit
d) continue
Answer: d
5. Which of this statement is incorrect?
a) switch statement is more efficient than a set of nested ifs
b) two case constants in the same switch can have identical values
c) switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression
d) it is possible to create a nested switch statements
Answer: b
6. What will be the output of the following Java program?
class selection_statements
{
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
a) 1
b) 2
c) 3
d) 4
Answer: b
7. What will be the output of the following Java program?
class comma_operator
{
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
a) 5
b) 6
c) 14
d) compilation error
Answer: b
8. What will be the output of the following Java program?
class jump_statments
{
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y)
{
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
Answer: c
9. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
final int a=10,b=20;
while(a<b)
{
System.out.println("Hello");
}
System.out.println("World");
}
}
a) Hello
b) run time error
c) Hello world
d) compile time error
Answer: d
10. What will be the output of the following Java program?
class Output
{
public static void main(String args[])
{
int a = 5;
int b = 10;
first:
{
second:
{
third:
{
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}
a) 5 10
b) 10 5
c) 5
d) 10
Answer: d
Java Questions & Answers – Control Statements – 2
1. What would be the output of the following code snippet if variable a=10?
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
a) 1 2
b) 2 3
c) 1 3
d) 3
Answer: d
2. The while loop repeats a set of code while the condition is not met?
a) True
b) False
Answer: b
3. What is true about a break?
a) Break stops the execution of entire program
b) Break halts the execution and forces the control out of the loop
c) Break forces the control out of the loop and starts the execution of next iteration
d) Break halts the execution of the loop for certain time frame
Answer: b
4. What is true about do statement?
a) do statement executes the code of a loop at least once
b) do statement does not get execute if condition is not matched in the first iteration
c) do statement checks the condition at the beginning of the loop
d) do statement executes the code more than once always
Answer: a
5. Which of the following is used with the switch statement?
a) Continue
b) Exit
c) break
d) do
Answer: c
6. What is the valid data type for variable “a” to print “Hello World”?
switch(a)
{
System.out.println("Hello World");
}
a) int and float
b) byte and short
c) char and long
d) byte and char
Answer: d
7. Which of the following is not a decision making statement?
a) if
b) if-else
c) switch
d) do-while
Answer: d
8. Which of the following is not a valid jump statement?
a) break
b) goto
c) continue
d) return
Answer: b
9. From where break statement causes an exit?
a) Only from innermost loop
b) Terminates a program
c) Only from innermost switch
d) From innermost loops or switches
Answer: d
10. Which of the following is not a valid flow control statement?
a) exit()
b) break
c) continue
d) return
Answer: a
Java Questions & Answers – Concepts of OOPs
1. Which of the following is not OOPS concept in Java?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
Answer: d
2. Which of the following is a type of polymorphism in Java?
a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
d) Multilevel polymorphism
Answer: a
3. When does method overloading is determined?
a) At run time
b) At compile time
c) At coding time
d) At execution time
Answer: b
4. When Overloading does not occur?
a) More than one method with same name but different method signature and different number or type of parameters
b) More than one method with same name, same signature but different number of signature
c) More than one method with same name, same signature, same number of parameters but different type
d) More than one method with same name, same number of parameters and type but different signature
Answer: d
5. Which concept of Java is a way of converting real world objects in terms of class?
a) Polymorphism
b) Encapsulation
c) Abstraction
d) Inheritance
Answer: c
6. Which concept of Java is achieved by combining methods and attribute into a class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
Answer: a
7. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer: d
8. What is it called where child object gets killed if parent object is killed?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer: b
9. What is it called where object has its own lifecycle and child object cannot belong to another parent object?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
Answer: a
10. Method overriding is combination of inheritance and polymorphism?
a) True
b) false
Answer: a
Java Questions & Answers – JDK-JRE-JIT-JVM
1. Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE
Answer: b
2. Which component is responsible for converting bytecode into machine specific code?
a) JVM
b) JDK
c) JIT
d) JRE
Answer: a
3. Which component is responsible to run java program?
a) JVM
b) JDK
c) JIT
d) JRE
Answer: d
4. Which component is responsible to optimize bytecode to machine code?
a) JVM
b) JDK
c) JIT
d) JRE
Answer: c
5. Which statement is true about java?
a) Platform independent programming language
b) Platform dependent programming language
c) Code dependent programming language
d) Sequence dependent programming language
Answer: a
6. Which of the below is invalid identifier with the main method?
a) public
b) static
c) private
d) final
Answer: c
7. What is the extension of java code files?
a) .class
b) .java
c) .txt
d) .js
Answer: b
8. What is the extension of compiled java classes?
a) .class
b) .java
c) .txt
d) .js
Answer: a
9. How can we identify whether a compilation unit is class or interface from a .class file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type
Answer: a
10. What is use of interpreter?
a) They convert bytecode to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
d) It is a synonym for JIT
Answer: b