Java Code-Based MCQs: Object Class, toString(), clone(), Control Statements & Arrays

Friday, September 4, 2026
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

  1. Read the code carefully.
  2. Choose the correct option.
  3. Do not use any string methods.
  4. Do not use any predefined array methods.
  5. Assume Java 21.
  6. For output questions, consider the exact execution flow.
  7. 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 = new Student();
        System.out.println(s);
    }
}

what is the output?

a) student:101
b) Student:101
c) Test@101
d) 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?

a)
id=10
id=10

b)
Student@10
Student@10

c)
id=10
Student@10

d) 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?

a) x=20
b) null
c) NullPointerException
d) 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.toString() here.

if we had written:

System.out.println(d.toString());

then it would cause NullPointerException.
Question 4
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?

a)
student-10
student-20

b)
student-20
student-10

c)
10
20

d) compilation error
View Answer & Explanation
answer: a)

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
Question 5
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?

a)
x=5
x=5

b)
x=5
x=15

c)
x=15
x=15

d) compilation error
View Answer & Explanation
answer: b)

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
Question 6
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?

a)
80
90

b)
90
90

c)
80
80

d) 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 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?

a) 80
b) 100
c) 0
d) 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 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?

a) true
b) false
c) null
d) 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 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?

a)
true
true

b)
false
false

c)
true
false

d)
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 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?

a) 50
b) 100
c) 0
d) 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?

a) 80
b) 90
c) 0
d) 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) a
b) b
c) no output
d) 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?

a) one
b) two
c) three
d) 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?

a) three
b) five
c) both
d) 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?

a)
two

b)
two
three
default

c)
one
two
three

d)
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) a
b) b
c) c
d) 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)
a
6

b)
b
6

c)
b
5

d)
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)
a
6

b)
b
6

c)
a
5

d)
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?

a) 10
b) 15
c) 16
d) 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?

a) 2
b) 4
c) 6
d) 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?

a)
1
2
3
4

b)
1
3

c)
2
4

d)
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?

a) no output
b) 5
c) 5 6
d) 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?

a)
1
2
3
4
5

b)
1
2
4
5

c)
3

d)
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?

a)
1
2

b)
1
2
3

c)
3
4
5

d)
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?

a)
10
20

b)
10
30

c)
20
30

d)
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?

a)
20
30

b)
100
30

c)
100
40

d)
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?

a) 20
b) 25
c) 30
d) 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?

a)
5
10
15
20

b)
20
15
10
5

c)
15
10
5

d)
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?

a) 2
b) 3
c) 4
d) 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?

a) 3
b) 7
c) 9
d) 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?

a) 2
b) 3
c) 4
d) 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?

a)
1
2
3
4
5

b)
1
4
3
8
5

c)
2
4
6
8
10

d)
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 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?

a)
marks=75
marks=75

b)
Student@...
Student@...

c)
marks=75
null

d) 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 toString():

return "marks=" + marks;

therefore both print:

marks=75
Question 34
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?

a) same
b) different
c) 60
d) 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?

a) 4
b) 8
c) 12
d) 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 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?

a)
student:80
student:80

b)
student:80
student:90

c)
student:90
student:90

d) compilation error
View Answer & Explanation
answer: b)

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
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?

a) 6
b) 9
c) 15
d) 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?

a)
15
25
30
40

b)
10
20
30
40

c)
15
25
35
45

d)
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 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?

a)
same object
50

b)
different object
10

c)
different object
50

d)
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?

a) 50
b) 55
c) 60
d) 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

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.