06 Sep
|
StudyECart Technologies
|
Gurugram
06 Sep
StudyECart Technologies
Gurugram
Code Crafters Assessment
Java Code-Based MCQ Test
Target Topics: Object Class Methods, toString(), 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 toString() { return "student:" + id; } } public class Test { public static void main(String[] args) { Student s = recent 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.toString(). because Student overrides toString(): return "student:" + id; id is 101. therefore: student:101
Question 2 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?
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.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
Question 3 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?
x=20
null
NullPointerException
compilation error
View Answer & Explanation
answer: b) null
explanation:
d is a null reference. when we execute: System.out.println(
📌 Java Code-Based MCQs: Object Class, toString(), clone(), Control Statements
🏢 StudyECart Technologies
📍 Gurugram