WorksheetsJava MCQs for Placement Preparation
Total questions: 20
Worksheet time: 11mins
1. int a = 10; int b = a; b += 5; System.out.println(a + " " + b);
a) 10 10
b) 10 15
c) 15 10
d) 15 15
String s1 = "Java"; String s2 = s1; s1 = s1.toUpperCase(); System.out.println(s1 + " " + s2);
a) JAVA JAVA
b) JAVA Java
c) Java JAVA
d) Java Java
int x = -5; if(x < 0) if(x % 2 == 0) System.out.println("Even Negative"); else System.out.println("Odd Negative"); else System.out.println("Positive");
Odd Negative
Even Negative
Positive
Compilation Error
int n = 1; switch(n){ case 1: System.out.print("A "); case 2: System.out.print("B "); default: System.out.print("C"); }
A
A B
A B C
C
int[] arr = {1, 2, 3, 4};
for(int x : arr) {
x *= 2;
}
for(int i = 0; i < arr.length; i++) {
arr[i] += 3;
}
System.out.println(Arrays.toString(arr));
[2, 4, 6, 8]
[4,5,6,7]
[3, 4, 5, 6]
[1, 2, 3, 4]
ArrayList
[A, B, C]
[A, C, B]
[C, A, B]
[A, B]
Set
true 3
true 4
false 3
false 4
Map
10 2
30 2
30 3
10 3
class A { void msg(){ System.out.println("A"); } } class B extends A { void msg(){ System.out.println("B"); } } class C extends B { } C obj = new C(); obj.msg();
A
B
C
Compilation Error
class Test { void show(int a){ System.out.println("int"); } void show(double a){ System.out.println("double"); } } new Test().show(5);
int
double
Compilation Error
Runtime Error
Recursion Output static int sum(int n){ if(n==0) return 0; return n + sum(n-1); } System.out.println(sum(3));
6
3
0
9
What will be the output if the following code is executed? class A { void msg(){ System.out.println("A"); } } class B extends A { void msg(){ System.out.println("B"); } } A obj = new B(); obj.msg();
B
A
A B
Compilation Error
ArrayList
[1,2,3]
[1,3]
[2,3]
Error
int[] arr = {1,2,3};
for(int i=0;i<arr.length/2;i++){
int temp = arr[i];
arr[i]=arr[arr.length-1-i];
arr[arr.length-1-i]=temp;
}
System.out.println(Arrays.toString(arr));
[1,2,3]
[3,2,1]
[3,1,2]
[2,1,3]
class A {void msg() { System.out.println("A"); }}
class B extends A {void msg() { System.out.println("B"); }}
List list = new ArrayList<>();
list.add(new B());
list.add(new A());
for(A obj : list) obj.msg();
a) A B
b) B A
c) Compile Error
d) Runtime Error
Match the OOP concepts with the correct description:
Encapsulation
Inheritance
Polymorphism
Abstraction
Descriptions:
(i) Hiding internal details and exposing essential features
(ii) Wrapping data + methods together
(iii) Using one interface to represent multiple forms
(iv) Acquiring properties of another class
Correct Mapping:
1-ii, 2-iv, 3-iii, 4-i
1-i, 2-ii, 3-iii, 4-iv
1-iv, 2-i, 3-ii, 4-iii
1-iii, 2-ii, 3-iv, 4-i
Which statement about abstraction is correct?
a) Abstraction is achieved using abstract classes and interfaces
b) Abstraction is hiding data inside a class using private variables
c) Abstraction is achieved only through constructors
d) Abstraction means method overloading
A
B
C
D
Why is String in Java considered immutable?
a) Because it is declared as final
b) Because changing a string creates a new object in the heap
c) Because it is a primitive type
d) Because it cannot be stored in heap memory
A
B
C
D
Which of the following is correct?
a) Encapsulation = hiding internal details, Abstraction = binding data & methods
b) Abstraction = hiding implementation, Encapsulation = restricting direct access
c) Both are exactly the same concept
d) Encapsulation can only be achieved using interfaces
A
B
C
D
class Test {
void show(int a, long b) { System.out.println("int-long"); }
void show(long a, int b) { System.out.println("long-int"); }
}
public class Main {
public static void main(String[] args) {
new Test().show(10, 10);
}
}
int-long
long-int
Compile-time Error
Runtime Error
