Java Code-Based MCQ Test
Target Topics: Object Class Methods, toString(), clone(), Control Statements, and Arrays
Instructions
- Read the code carefully.
- Choose the correct option.
- Do not use any string methods.
- Do not use any predefined array methods.
- Assume Java 21.
- For output questions, consider the exact execution flow.
- Select only one answer unless mentioned otherwise.
class Student {
int id = 101;
@Override
public String toString() {
return "student:" + id;
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s);
}
}
what is the output?
View Answer & Explanation
explanation:
System.out.println(s) internally calls s.toString(). because Student overrides toString(): return "student:" + id; id is 101. therefore: student:101
class Student {
int id = 10;
@Override
public String toString() {
return "id=" + id;
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.toString());
System.out.println(s);
}
}
what is printed?
View Answer & Explanation
output:
id=10
id=10
explanation:
System.out.println(s.toString()); directly calls toString(). System.out.println(s); also internally calls toString() for a non-null object. since Student overrides toString(), both statements produce: id=10
class Demo {
int x = 20;
@Override
public String toString() {
return "x=" + x;
}
}
public class Test {
public static void main(String[] args) {
Demo d = null;
System.out.println(d);
}
}
what happens?
View Answer & Explanation
explanation:
d is a null reference. when we execute: System.out.println(d); println(Object) handles a null reference and prints: null there is no direct call to d.toString() here. if we had written: System.out.println(d.toString()); then it would cause NullPointerException.
class Student {
int id;
Student(int id) {
this.id = id;
}
@Override
public String toString() {
return "student-" + id;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(10);
Student s2 = new Student(20);
System.out.println(s1);
System.out.println(s2);
}
}
what is the output?
View Answer & Explanation
output:
student-10
student-20
explanation:
s1 contains id 10. s2 contains id 20. toString() uses the current id value: return "student-" + id; therefore: s1 → student-10 s2 → student-20
class Demo {
int x = 5;
@Override
public String toString() {
return "x=" + x;
}
}
public class Test {
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d);
d.x = 15;
System.out.println(d);
}
}
what is the output?
View Answer & Explanation
output:
x=5
x=15
explanation:
toString() does not permanently store the value "x=5". it reads the current value of x whenever it is called. first: x = 5 so: x=5 then: d.x = 15; when toString() is called again: x=15
class Student implements Cloneable {
int marks = 80;
@Override
public String toString() {
return "marks=" + marks;
}
public Student cloneStudent() throws CloneNotSupportedException {
return (Student) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
s2.marks = 90;
System.out.println(s1.marks);
System.out.println(s2.marks);
}
}
what is the output?
View Answer & Explanation
output:
80
90
explanation:
super.clone() creates a new Student object. therefore: s1 != s2 both initially contain: marks = 80 then: s2.marks = 90; only s2 changes. so: s1.marks → 80 s2.marks → 90 this is cloning, not reference assignment.
class Student {
int marks = 80;
public Student cloneStudent() {
return this;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
s2.marks = 100;
System.out.println(s1.marks);
}
}
what is the output?
View Answer & Explanation
explanation:
this method: return this; does NOT create a new object. therefore: s1 ↓ Student object s2 ↓ same Student object when: s2.marks = 100; the same object is modified. therefore: s1.marks = 100
class Student implements Cloneable {
int marks = 80;
public Student cloneStudent()
throws CloneNotSupportedException {
return (Student) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
System.out.println(s1 == s2);
}
}
what is the output?
View Answer & Explanation
explanation:
clone() creates a new object. so: s1 → original object s2 → cloned object they contain the same field values, but they are different objects. therefore: s1 == s2 is: false
class Student implements Cloneable {
int marks = 70;
public Student cloneStudent()
throws CloneNotSupportedException {
return (Student) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
System.out.println(s1.marks == s2.marks);
System.out.println(s1 == s2);
}
}
what is the output?
View Answer & Explanation
output:
true
false
explanation:
both objects initially have: marks = 70 so: s1.marks == s2.marks means: 70 == 70 which is true. but: s1 == s2 checks whether both references point to the exact same object. clone() creates a separate object. therefore: true false
class Student implements Cloneable {
int marks = 50;
public Student cloneStudent()
throws CloneNotSupportedException {
return (Student) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
s1.marks = 100;
System.out.println(s2.marks);
}
}
what is the output?
View Answer & Explanation
explanation:
clone() creates an independent Student object. initially: s1.marks = 50 s2.marks = 50 then: s1.marks = 100; only s1 changes. s2 remains: 50
class Student {
int marks = 80;
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = s1;
s2.marks = 90;
System.out.println(s1.marks);
}
}
what is the output?
View Answer & Explanation
explanation:
this statement: Student s2 = s1; does not create another Student object. both references point to the same object. therefore: s1 → same object ← s2 when: s2.marks = 90; the object changes. so: s1.marks = 90
public class Test {
public static void main(String[] args) {
int x = 10;
if (x > 5)
if (x > 15)
System.out.println("a");
else
System.out.println("b");
}
}
what is the output?
View Answer & Explanation
explanation:
x = 10 first condition: x > 5 true. then: x > 15 false. therefore the else belongs to the nearest if: if (x > 15) so: b
public class Test {
public static void main(String[] args) {
int x = 10;
if (x > 5) {
if (x < 20) {
System.out.println("one");
} else {
System.out.println("two");
}
} else {
System.out.println("three");
}
}
}
what is the output?
View Answer & Explanation
explanation:
x = 10 first: x > 5 true. then: x < 20 also true. therefore: one
public class Test {
public static void main(String[] args) {
int x = 15;
if (x % 3 == 0) {
if (x % 5 == 0) {
System.out.println("both");
} else {
System.out.println("three");
}
} else {
System.out.println("none");
}
}
}
what is the output?
View Answer & Explanation
explanation:
x = 15 15 % 3 == 0 true. then: 15 % 5 == 0 also true. therefore: both
public class Test {
public static void main(String[] args) {
int x = 2;
switch (x) {
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
default:
System.out.println("default");
}
}
}
what is the output?
View Answer & Explanation
output:
two
three
default
explanation:
x = 2. execution starts at: case 2 there is no break after case 2. therefore execution continues into: case 3 and then: default this is called switch fall-through.
public class Test {
public static void main(String[] args) {
int x = 3;
switch (x) {
case 1:
System.out.println("a");
break;
case 2:
System.out.println("b");
break;
case 3:
System.out.println("c");
break;
default:
System.out.println("d");
}
}
}
what is the output?
View Answer & Explanation
explanation:
x = 3. therefore: case 3 executes. then: break; stops the switch. output: c
public class Test {
public static void main(String[] args) {
int x = 5;
if (x++ > 5) {
System.out.println("a");
} else {
System.out.println("b");
}
System.out.println(x);
}
}
what is the output?
View Answer & Explanation
output:
b
6
explanation:
condition: x++ > 5 post-increment means: 1. use current value 2. increment afterwards current x = 5. comparison: 5 > 5 false. therefore: b after the condition, x becomes: 6
public class Test {
public static void main(String[] args) {
int x = 5;
if (++x > 5) {
System.out.println("a");
} else {
System.out.println("b");
}
System.out.println(x);
}
}
what is the output?
View Answer & Explanation
output:
a
6
explanation:
condition: ++x > 5 pre-increment means x is increased first. x becomes: 6 then: 6 > 5 true. therefore: a final x: 6
public class Test {
public static void main(String[] args) {
int x = 1;
for (int i = 1; i <= 5; i++) {
x = x + i;
}
System.out.println(x);
}
}
what is the output?
View Answer & Explanation
explanation:
initial: x = 1 loop: i = 1 → x = 2 i = 2 → x = 4 i = 3 → x = 7 i = 4 → x = 11 i = 5 → x = 16 therefore: 16
public class Test {
public static void main(String[] args) {
int x = 10;
for (int i = 0; i < 3; i++) {
x = x - 2;
}
System.out.println(x);
}
}
what is the output?
View Answer & Explanation
explanation:
initial: x = 10 loop executes 3 times. first: 10 - 2 = 8 second: 8 - 2 = 6 third: 6 - 2 = 4 therefore: 4
public class Test {
public static void main(String[] args) {
int i = 1;
while (i < 5) {
System.out.println(i);
i += 2;
}
}
}
what is the output?
View Answer & Explanation
output:
1
3
explanation:
initial: i = 1 condition: 1 < 5 → true print 1. then: i = 3 3 < 5 → true print 3. then: i = 5 5 < 5 → false. loop stops.
public class Test {
public static void main(String[] args) {
int i = 5;
do {
System.out.println(i);
i++;
} while (i < 5);
}
}
what is the output?
View Answer & Explanation
explanation:
do-while always executes the body at least once. initial: i = 5 body executes: print 5 then: i++ i becomes 6. condition: 6 < 5 false. therefore: 5
public class Test {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
what is the output?
View Answer & Explanation
output:
1
2
4
5
explanation:
when i becomes 3: if (i == 3) continue; continue skips the remaining statements of the current iteration. therefore 3 is not printed. 4 and 5 continue normally.
public class Test {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
}
}
what is the output?
View Answer & Explanation
output:
1
2
explanation:
i = 1 → print 1 i = 2 → print 2 i = 3 → condition: i == 3 true. break immediately terminates the loop. therefore 3, 4 and 5 are not printed.
public class Test {
public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
System.out.println(a[0]);
System.out.println(a[2]);
}
}
what is the output?
View Answer & Explanation
output:
10
30
explanation:
array indexes start from 0. array: index: 0 1 2 3 value: 10 20 30 40 therefore: a[0] → 10 a[2] → 30
public class Test {
public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
a[1] = 100;
System.out.println(a[1]);
System.out.println(a[2]);
}
}
what is the output?
View Answer & Explanation
output:
100
30
explanation:
initial array: 10 20 30 40 statement: a[1] = 100; changes only index 1. new array: 10 100 30 40 therefore: a[1] → 100 a[2] → 30
public class Test {
public static void main(String[] args) {
int[] a = {2, 4, 6, 8, 10};
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum = sum + a[i];
}
System.out.println(sum);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 2 4 6 8 10 sum: 0 + 2 = 2 2 + 4 = 6 6 + 6 = 12 12 + 8 = 20 20 + 10 = 30 therefore: 30
public class Test {
public static void main(String[] args) {
int[] a = {5, 10, 15, 20};
for (int i = a.length - 1; i >= 0; i--) {
System.out.println(a[i]);
}
}
}
what is the output?
View Answer & Explanation
output:
20
15
10
5
explanation:
starting index:
a.length - 1
array length is 4.
last index:
3
therefore traversal is:
3 → 2 → 1 → 0
values:
40 → 30 → 20 → 10
wait — important correction:
for the given array:
int[] a = {5, 10, 15, 20};
the indexes are:
0 → 5
1 → 10
2 → 15
3 → 20
therefore the actual output is:
20
15
10
5
so answer remains:
b
public class Test {
public static void main(String[] args) {
int[] a = {10, 20, 30, 40, 50};
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > 25) {
count++;
}
}
System.out.println(count);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 10 20 30 40 50 condition: a[i] > 25 values satisfying it: 30 40 50 total: 3
public class Test {
public static void main(String[] args) {
int[] a = {3, 7, 2, 9, 4};
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
System.out.println(max);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 3 7 2 9 4 initial: max = 3 compare: 7 > 3 → max = 7 2 > 7 → false 9 > 7 → max = 9 4 > 9 → false final: max = 9
public class Test {
public static void main(String[] args) {
int[] a = {3, 7, 2, 9, 4};
int min = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
}
}
System.out.println(min);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 3 7 2 9 4 initial: min = 3 7 < 3 → false 2 < 3 → true min = 2 9 < 2 → false 4 < 2 → false final: 2
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
a[i] = a[i] * 2;
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
what is the output?
View Answer & Explanation
output:
1
4
3
8
5
explanation:
array: 1 2 3 4 5 condition: value % 2 == 0 only even values are modified. 2 becomes: 2 * 2 = 4 4 becomes: 4 * 2 = 8 final array: 1 4 3 8 5
class Student implements Cloneable {
int marks = 75;
@Override
public String toString() {
return "marks=" + marks;
}
public Student cloneStudent()
throws CloneNotSupportedException {
return (Student) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
System.out.println(s1);
System.out.println(s2);
}
}
what is the output?
View Answer & Explanation
output:
marks=75
marks=75
explanation:
s2 is a clone of s1. both objects initially have: marks = 75 Student overrides toString(): return "marks=" + marks; therefore both print: marks=75
class Student implements Cloneable {
int marks = 60;
public Student cloneStudent()
throws CloneNotSupportedException {
return (Student) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
s2.marks = 90;
if (s1.marks == s2.marks) {
System.out.println("same");
} else {
System.out.println("different");
}
}
}
what is the output?
View Answer & Explanation
explanation:
clone() creates a separate Student object. initial: s1.marks = 60 s2.marks = 60 then: s2.marks = 90; now: s1.marks = 60 s2.marks = 90 therefore: s1.marks == s2.marks is false. output: different
public class Test {
public static void main(String[] args) {
int[] a = {2, 4, 6, 8};
int result = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] % 4 == 0) {
result = result + a[i];
} else {
result = result - a[i];
}
}
System.out.println(result);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 2 4 6 8 start: result = 0 2 is not divisible by 4: result = 0 - 2 result = -2 4 is divisible by 4: result = -2 + 4 result = 2 6 is not divisible by 4: result = 2 - 6 result = -4 8 is divisible by 4: result = -4 + 8 result = 4 final: 4
⭐ Bonus Challenge Questions
class Student implements Cloneable {
int marks = 80;
@Override
public String toString() {
return "student:" + marks;
}
public Student cloneStudent()
throws CloneNotSupportedException {
Student copy = (Student) super.clone();
copy.marks = copy.marks + 10;
return copy;
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Student s1 = new Student();
Student s2 = s1.cloneStudent();
System.out.println(s1);
System.out.println(s2);
}
}
what is the output?
View Answer & Explanation
output:
student:80
student:90
explanation:
clone creates a separate object. original: s1.marks = 80 inside cloneStudent(): Student copy = (Student) super.clone(); copy.marks = copy.marks + 10; so only the cloned object becomes: 90 therefore: s1 → 80 s2 → 90
public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int result = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
continue;
}
result = result + a[i];
}
System.out.println(result);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 1 2 3 4 5 even numbers are skipped using continue. 1 → added 2 → skipped 3 → added 4 → skipped 5 → added result: 1 + 3 + 5 = 9
public class Test {
public static void main(String[] args) {
int[] a = {10, 20, 30, 40};
for (int i = 0; i < a.length; i++) {
if (a[i] == 30) {
break;
}
a[i] = a[i] + 5;
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
what is the output?
View Answer & Explanation
output:
15
25
30
40
explanation:
initial: 10 20 30 40 i = 0: 10 == 30 → false 10 becomes 15. i = 1: 20 == 30 → false 20 becomes 25. i = 2: 30 == 30 → true break executes. therefore no modification happens to 30 or 40. final array: 15 25 30 40
class Demo implements Cloneable {
int x = 10;
public Demo cloneDemo()
throws CloneNotSupportedException {
return (Demo) super.clone();
}
}
public class Test {
public static void main(String[] args)
throws CloneNotSupportedException {
Demo d1 = new Demo();
Demo d2 = d1.cloneDemo();
d1.x = 50;
if (d1 == d2) {
System.out.println("same object");
} else {
System.out.println("different object");
}
System.out.println(d2.x);
}
}
what is the output?
View Answer & Explanation
output:
different object
10
explanation:
clone() creates a new object. therefore: d1 == d2 is false. then: d1.x = 50; only d1 changes. d2 still contains: 10 therefore: different object 10
public class Test {
public static void main(String[] args) {
int[] a = {5, 10, 15, 20, 25};
int result = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > 10) {
if (a[i] % 5 == 0) {
result = result + a[i];
}
} else {
result = result - a[i];
}
}
System.out.println(result);
}
}
what is the output?
View Answer & Explanation
explanation:
array: 5 10 15 20 25 initial: result = 0 5: 5 > 10 → false result = 0 - 5 result = -5 10: 10 > 10 → false result = -5 - 10 result = -15 15: 15 > 10 → true 15 % 5 == 0 → true result = -15 + 15 result = 0 20: 20 > 10 → true 20 % 5 == 0 → true result = 0 + 20 result = 20 25: 25 > 10 → true 25 % 5 == 0 → true result = 20 + 25 result = 45 IMPORTANT: the correct result is: 45 so none of the given options are correct. this question should be corrected before giving it to candidates. correct option: e) 45
Quick Answer Key
Important Concepts Tested
toString(): object reference passed to println can result in toString() being called. clone(): super.clone() creates a separate object with shallow field copying. reference assignment: Student s2 = s1; does not clone the object. == : checks whether two object references point to the same object. primitive ==: compares primitive values. arrays: index starts from 0. last valid index: array.length - 1 continue: skips the current iteration. break: terminates the loop/switch. do-while: executes at least once. switch: without break, execution can fall through to the next case. important: question 40 has been intentionally identified as having an incorrect option set. the actual answer is 45.