Java MCQ - Sample
Java MCQ - Sample
1 Datatypes 51
2 Control Statement 25
3 Loops 15
4 OOPS 13
5 Classes 56
6 Inheritance 11
7 Overloading - Overrriding 29
8 Abstract Class - Method 31
9 Interface 27
10 Access Control - Package 22
11 Threading 20
12 Collection 50
13 IO Classes 26
14 String 32
15 Exception 15
16 Spring 20
17 Struts 10
18 Serialization 12
19 JSON 5
20 Misc 30
Total 500
Questions
In Java Arrays are
class Abc
{
public static void main(String[]args)
{
String[] elements = { "first item", "second item", "third item" };
String first = (elements.length > 0) ? elements[0]: null;
}
}
enum Season {
WINTER, SPRING, SUMMER, FALL
};
System.out.println(Season.WINTER.ordinal());
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
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;
}
}
1 0 both
0 1 2
11 0 1 1.5 1
25 24 32
11 11 10 10 11 10
D) Answer
None of the above A. Objects
None B.False
3 A.0
1 2 3 4 5 6 7 8 9 10 B.1 2 3 4 5
“Hello D. “Hello
world” world”
Code runs successfully A. ArrayStoreException
3&2 A. 1, 2 & 3
None D.none
33 C.32
10 11 C. 11 10
Questions
if(a<=0)
{
if(a==0)
{
System.out.println("1 ");
}
else
{
System.out.println("2 ");
}
}
System.out.println("3 ");
The while loop repeats a set of code while the condition is not met?
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);
}
}
class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
What is the prototype of the default constructor of this class?
public class prototype { }
If an expression contains double, int, float, long, then whole expression will promoted into
which of these data types?
public class Test {
public static void main (String args []) {
boolean a = false;
if (a = true)
System.out.println("Hello");
else
System.out.println("Goodbye");
}
}
class Test{
public static void main(String args[]){
int x=7;
if(x==2);
System.out.println("NumberSeven");
System.out.println("NotSeven");
}
}
char ch = 'a';
switch (ch){
case 'a':
case 'A': System.out.print(ch); break;
case 'b':
case 'B': System.out.print(ch); break;
case 'c':
case 'C': System.out.print(ch); break;
case 'd':
case 'D': System.out.print(ch);
}
f(number>=0)
if(number>0)
system.out.println("Number is positive");
else
system.out.println("Number is negative");
char c='a';
switch (c)
{
case 'a';
system.out.println("A");
case 'b';
system.out.println("B");
default;
system.out.println("C");
}
A) B)
1 2 2 3
1 0
25 15
do-while while
1 2
25 24
prototype( ) prototype(void)
long int
Program produces no output but
terminates correctly. Program does not terminate.
1 2
abcd aa
Number is negative Number is positive
1 3 3
5 error
for none
3 4
32 33
double float
Prints out "Hello" Prints out "Goodbye"
3 1 2
NotSeven Error
a ab
Both A and B None of the above
D. 3
B. False
A. 25
A. do-while
B. 2
C. 32
D. public prototype( )
C. double
C.Prints out "Hello"
D. 1 2
A.NumberSeven NotSeven
C.a
A) Number is negative
Which concept of Java is a way of converting real world objects in terms of class?
Which concept of Java is achieved by combining methods and attribute into a class?
What is it called if an object has its own lifecycle and there is no owner?
What is it called where child object gets killed if parent object is killed?
What is it called where object has its own lifecycle and child object cannot belong to another
parent object?
class Test {
int a;
public int b;
private int c;
}
class AcessTest {
public static void main(String args[])
{
Test ob = new Test();
ob.a = 10;
ob.b = 20;
ob.c = 30;
System.out.println(" Output :a, b, and c" + ob.a + " " + ob.b + " " + ob.c);
}
}
A) B) C)
Inheritance Encapsulation Polymorphism
1 0
Binds together code and
Reduce Complexity data Parallelism
Compilation D. Compilation
Inheritance C.Abstraction
Abstration A.Encapsulation
Association B. Composition
Association
A. Aggregation
A. true