Java Code-Based MCQs: Object Class, toString(), clone(), Control Statements & Arrays
StudyECart Technologies Pvt Ltd ยท Gurugram, Haryana, India ยท Full Time ยท Onsite
Posted
Code Crafters Assessment Java Code-Based MCQ Test Target Topics: Object Class Methods, to String(), clone(), Control Statements, and Arrays ๐ Total: 35 + 5 Bonus MCQs โ๏ธ Difficulty: Medium to Hard โ Runtime: Java 21 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. Question 1 class Student { int id = 101; @Override public String to String() { return "student:" + id; } } public class Test { public static void main(String[] args) { Student s = new Student(); System.out.println(s); } } what is the output? student:101 Student:101 Test@101 compilation error View Answer & Explanation answer: a) student:101 explanation: System.out.println(s) internally calls s.to String(). because Student overrides to String(): return "student:" + id; id is 101. therefore: student:101 Question 2 class Student { int id = 10; @Override public String to String() { return "id=" + id; } } public class Test { public static void main(String[] args) { Student s = new Student(); System.out.println(s.to String()); System.out.println(s); } } what is printed? id=10 id=10 Student@10 Student@10 id=10 Student@10 compilation error View Answer & Explanation answer: a) output: id=10 id=10 explanation: System.out.println(s.to String()); directly calls to String(). System.out.println(s); also internally calls to String() for a non-null object. since Student overrides to String(), both statements produce: id=10 Question 3 class Demo { int x = 20; @Override public String to String() { return "x=" + x; } } public class Test { public static void main(String[] args) { Demo d = null; System.out.println(d); } } what happens? x=20 null Null Pointer Exception compilation error View Answer & Explanation answer: b) null 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.to String() here. if we had written: System.out.println(d.to String()); then it would cause Null Pointer Exception. Question 4 class Student { int id; Student(int id) { this.id = id; } @Override public String to String() { 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? student-10 student-20 student-20 student-10 10 20 compilation error View Answer & Explanation answer: a) output: student-10 student-20 explanation: s1 contains id 10. s2 contains id 20. to String() uses the current id value: return "student-" + id; therefore: s1 โ student-10 s2 โ student-20 Question 5 class Demo { int x = 5; @Override public String to String() { 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? x=5 x=5 x=5 x=15 x=15 x=15 compilation error View Answer & Explanation answer: b) output: x=5 x=15 explanation: to String() 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 to String() is called again: x=15 Question 6 class Student implements Cloneable { int marks = 80; @Override public String to String() { return "marks=" + marks; } public Student clone Student() throws Clone Not Supported Exception { return (Student) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); s2.marks = 90; System.out.println(s1.marks); System.out.println(s2.marks); } } what is the output? 80 90 90 90 80 80 compilation error View Answer & Explanation answer: a) 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. Question 7 class Student { int marks = 80; public Student clone Student() { return this; } } public class Test { public static void main(String[] args) { Student s1 = new Student(); Student s2 = s1.clone Student(); s2.marks = 100; System.out.println(s1.marks); } } what is the output? 80 100 0 compilation error View Answer & Explanation answer: b) 100 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 Question 8 class Student implements Cloneable { int marks = 80; public Student clone Student() throws Clone Not Supported Exception { return (Student) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); System.out.println(s1 == s2); } } what is the output? true false null compilation error View Answer & Explanation answer: b) false 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 Question 9 class Student implements Cloneable { int marks = 70; public Student clone Student() throws Clone Not Supported Exception { return (Student) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); System.out.println(s1.marks == s2.marks); System.out.println(s1 == s2); } } what is the output? true true false false true false false true View Answer & Explanation answer: c) 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 Question 10 class Student implements Cloneable { int marks = 50; public Student clone Student() throws Clone Not Supported Exception { return (Student) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); s1.marks = 100; System.out.println(s2.marks); } } what is the output? 50 100 0 compilation error View Answer & Explanation answer: a) 50 explanation: clone() creates an independent Student object. initially: s1.marks = 50 s2.marks = 50 then: s1.marks = 100; only s1 changes. s2 remains: 50 Question 11 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? 80 90 0 compilation error View Answer & Explanation answer: b) 90 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 Question 12 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? a b no output compilation error View Answer & Explanation answer: b) b 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 Question 13 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? one two three no output View Answer & Explanation answer: a) one explanation: x = 10 first: x > 5 true. then: x < 20 also true. therefore: one Question 14 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? three five both none View Answer & Explanation answer: c) both explanation: x = 15 15 % 3 == 0 true. then: 15 % 5 == 0 also true. therefore: both Question 15 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? two two three default one two three default View Answer & Explanation answer: b) 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. Question 16 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? a b c d View Answer & Explanation answer: c) c explanation: x = 3. therefore: case 3 executes. then: break; stops the switch. output: c Question 17 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? a 6 b 6 b 5 a 5 View Answer & Explanation answer: b) 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 Question 18 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? a 6 b 6 a 5 b 5 View Answer & Explanation answer: a) 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 Question 19 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? 10 15 16 20 View Answer & Explanation answer: c) 16 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 Question 20 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? 2 4 6 8 View Answer & Explanation answer: c) 4 explanation: initial: x = 10 loop executes 3 times. first: 10 - 2 = 8 second: 8 - 2 = 6 third: 6 - 2 = 4 therefore: 4 Question 21 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? 1 2 3 4 1 3 2 4 1 3 5 View Answer & Explanation answer: b) 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. Question 22 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? no output 5 5 6 infinite loop View Answer & Explanation answer: b) 5 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 Question 23 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? 1 2 3 4 5 1 2 4 5 3 1 2 View Answer & Explanation answer: b) 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. Question 24 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? 1 2 1 2 3 3 4 5 1 2 4 5 View Answer & Explanation answer: a) 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. Question 25 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? 10 20 10 30 20 30 20 40 View Answer & Explanation answer: b) 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 Question 26 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? 20 30 100 30 100 40 20 100 View Answer & Explanation answer: b) 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 Question 27 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? 20 25 30 35 View Answer & Explanation answer: c) 30 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 Question 28 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? 5 10 15 20 20 15 10 5 15 10 5 20 10 5 View Answer & Explanation answer: b) 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 Question 29 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? 2 3 4 5 View Answer & Explanation answer: b) 3 explanation: array: 10 20 30 40 50 condition: a[i] > 25 values satisfying it: 30 40 50 total: 3 Question 30 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? 3 7 9 4 View Answer & Explanation answer: c) 9 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 Question 31 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? 2 3 4 7 View Answer & Explanation answer: a) 2 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 Question 32 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? 1 2 3 4 5 1 4 3 8 5 2 4 6 8 10 1 2 6 4 10 View Answer & Explanation answer: b) 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 Question 33 class Student implements Cloneable { int marks = 75; @Override public String to String() { return "marks=" + marks; } public Student clone Student() throws Clone Not Supported Exception { return (Student) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); System.out.println(s1); System.out.println(s2); } } what is the output? marks=75 marks=75 Student@... Student@... marks=75 null compilation error View Answer & Explanation answer: a) output: marks=75 marks=75 explanation: s2 is a clone of s1. both objects initially have: marks = 75 Student overrides to String(): return "marks=" + marks; therefore both print: marks=75 Question 34 class Student implements Cloneable { int marks = 60; public Student clone Student() throws Clone Not Supported Exception { return (Student) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); s2.marks = 90; if (s1.marks == s2.marks) { System.out.println("same"); } else { System.out.println("different"); } } } what is the output? same different 60 compilation error View Answer & Explanation answer: b) different 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 Question 35 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? 4 8 12 0 View Answer & Explanation answer: a) 4 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 Bonus Challenge Question 36 class Student implements Cloneable { int marks = 80; @Override public String to String() { return "student:" + marks; } public Student clone Student() throws Clone Not Supported Exception { Student copy = (Student) super.clone(); copy.marks = copy.marks + 10; return copy; } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Student s1 = new Student(); Student s2 = s1.clone Student(); System.out.println(s1); System.out.println(s2); } } what is the output? student:80 student:80 student:80 student:90 student:90 student:90 compilation error View Answer & Explanation answer: b) output: student:80 student:90 explanation: clone creates a separate object. original: s1.marks = 80 inside clone Student(): Student copy = (Student) super.clone(); copy.marks = copy.marks + 10; so only the cloned object becomes: 90 therefore: s1 โ 80 s2 โ 90 Bonus Challenge Question 37 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? 6 9 15 10 View Answer & Explanation answer: b) 9 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 Bonus Challenge Question 38 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? 15 25 30 40 10 20 30 40 15 25 35 45 15 20 30 40 View Answer & Explanation answer: a) 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 Bonus Challenge Question 39 class Demo implements Cloneable { int x = 10; public Demo clone Demo() throws Clone Not Supported Exception { return (Demo) super.clone(); } } public class Test { public static void main(String[] args) throws Clone Not Supported Exception { Demo d1 = new Demo(); Demo d2 = d1.clone Demo(); 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? same object 50 different object 10 different object 50 same object 10 View Answer & Explanation answer: b) 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 Bonus Challenge Question 40 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? 50 55 60 65, explanations and answers "code crafters โ java code based mcq View Answer & Explanation answer: b) 55 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 1 - a 2 - a 3 - b 4 - a 5 - b 6 - a 7 - b 8 - b 9 - c 10 - a 11 - b 12 - b 13 - a 14 - c 15 - b 16 - c 17 - b 18 - a 19 - c 20 - c 21 - b 22 - b 23 - b 24 - a 25 - b 26 - b 27 - c 28 - b 29 - b 30 - c 31 - a 32 - b 33 - a 34 - b 35 - a 36 - b 37 - b 38 - a 39 - b 40 - e (45) Important Concepts Tested to String(): object reference passed to println can result in to String() 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.