Java MCQ
Java MCQ
Java MCQ
class A{}
a. Class will not compile
b. Class will compile but can not be executed directly.
c. Class will compile but can not be executed directly.
d. Both B & C
Answer: B
Answer: A
om
b. Java was invented by James Gosling.
c. Java uses Interpreter for execution of program.
r.c
d. Both B & C.
Answer: D de
co
Answer: D
.c
w
5. What will happen if static modifier is removed from the signature of the public static void main(String
w
arg[]) method?
w
a. Compilation Error.
b. Program will compile but give run-time error.
c. Program will compile and run without any output.
d. Program will compile and run to show the required output.
Answer: B
6. In order for a java source code file, containing the public class Test, to successfully compile, which of
the following must be true?
a. It must have only one class in it.
b. It must be named Test.java.
c. It must import java.lang package.
d. Both A & B
Answer: B
7. The following class is saved in a file named as Hello.java. Which of the following is TRUE about it?
public class A{}
a. It can be compiled using command javac A.java
b. It must be saved in a file named as A.java, otherwise it will not compile.
c. It will be compiled using command javac A.java
d. None of These
Answer: B
Answer: A
9. All the objects in Java are created inside ______ and local variable in ______.
a. Class Area, Heap
b. Heap, Class Area
c. Heap, Stack
d. Class Area, Stack
om
Answer: C
10. Which of the following is the minimum requirement for executing a java Program?
r.c
a. JDK
b. JVM de
c. JRE
d. Both JDK and JRE
co
Answer: C
or
System.out.println(-112>>3);
A. -13
od
B. -14
C. 13
.c
D. JRE
w
Answer: B
w
w
Answer: B
13. Which of the following keyword is reserved but not used in Java?
a. const
b. goto
c. default
d. Both A & B
Answer: D
14. What will be the output of the following code snippet?
int i=10, j=5;
while(--i>10 && ++j>5);
System.out.println(i+””+j);
a. 95
b. 96
c. 105
d. Compilation Error
Answer: A
Answer: C
om
16. What will be the output of the following code:
r.c
int i= 5, j=2;
for( ; i-- > ++j ; ) de
System.out.println(i+””+j);
a. 34
co
b. 43
c. 53
or
d. Compilation Error
ef
Answer: B
od
a. true
b. false
w
c. truefalsetrue
w
d. Compilation Error
Answer: B
Answer: D
Answer: C
20. Which of the following keywords are NOT allowed with local variables in Java? i.
private ii. static iii. Final
a. only i
b. i & ii
c. i & iii
d. All
Answer: B
Answer: B
om
22. What will be the output of the following code snippet?
r.c
int x=10, y=5;
x = --y + x-- - --x + y; de
System.out.println(x);
a. 10
co
b. 9
c. 11
or
d. 14
ef
Answer: A
od
.c
23. If class Y is a subclass of X, Which of the following statement will return true?
w
X x1 = new Y();
a. x1 != null;
w
b. x1 instanceof Y
w
c. Both A & B
d. None of These
Answer: C
24. Which of the following keyword is used to declare a class variable in Java?
a. private
b. final
c. static
d. default
Answer: C
25. Which of the following keyword is used to restrict the accessibility of a method within the enclosing
class?
a. Private
b. final
c. static
d. None of These
Answer: A
Answer: B
om
Answer: B
r.c
28. Which of these is selection statement in Java? de
a. if()
b. for()
co
c. continue
d. break
or
Answer: A
ef
29. Which of the following loops will execute the body of loop even when condition controlling the loop is
od
initially false?
a. while
.c
b. for()
w
c. do-while
w
Answer: C
30. Which of these jump statements can skip processing remainder of code in its body for a particular
iteration?
a. break
b. exit
c. return
d. continue
Answer: D
om
Answer: B
r.c
33. What is the output of this program?
class comma_operator {
de
public static void main(String args[])
co
{
int sum = 0;
or
System.out.println(sum);
od
}
}
.c
A. 5
w
B. 6
C. 14
w
D. compilation error
w
Answer: B
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. 1357
b. 2468
c. 13579
d. 123456789
Answer: C
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
om
System.out.println(++y);
}
}
r.c
A. 1
B. 2 de
C. Runtime error owing to division by zero in if condition.
D. Unpredictable behavior of program.
co
Answer: B
or
class Output {
public static void main(String args[])
.c
{
int a = 5;
w
int b = 10;
w
first: {
w
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
37. switch(x)
{
default:
System.out.println("Hello");
}
Which two are acceptable types for x?
byte
long
char
float
Short
Long
a. byte and char
b. long and float
c. char and Short
d. float and Long
Answer: A
om
38. public void test(int x)
{
int odd = 1;
r.c
if(odd)
{ de
System.out.println("odd");
}
co
else
{
or
System.out.println("even");
ef
}
}
od
d. "odd" will be output for odd values of x, and "even" for even values.
w
Answer: A
41. In java, ............ can only test for equality, where as .......... can evaluate any type of the Boolean
expression.
a. switch, if
b. if, switch
c. if, break
d. break, if
Answer: A
Answer: D
om
43. What will be the output:
class Continue {
public static void main(String args[]){
r.c
for(int i=0; i<5; i++) {
System.out.println(i + " "); de
if (i%2 == 0) continue;
System.out.println("No Continue");
co
}
}
or
}
ef
a. 0
1
od
No Continue
2
.c
No Continue
w
3
w
4
w
b. 0
1
No Continue
2
3
No Continue
4
c. 0
No Continue
1
2
No Continue
3
4
d. compilation error
Answer: B
44. More than one statement in the …………… and ………….. portions of the for loop can be used.
a. condition, iteration
b. initialization, condition
c. initialization, iteration
d. Nove of the above
Answer: C
om
a. -1
b. 0
r.c
c. 1
d. 2 de
Answer: B
co
3. i++;
4. }
od
5. System.out.print(i);
a. 11
.c
b. 12
w
c. 13
w
d. 14
w
Answer: C
Answer: A