0% found this document useful (0 votes)
271 views4 pages

MCQs - Problems On Loops - 2 PDF

This document contains 5 multiple choice questions about loops in Java. The questions test understanding of for loops, nested for loops, and while loops. Each question includes 4 possible answer choices and the correct answer is provided at the end.

Uploaded by

Ashish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views4 pages

MCQs - Problems On Loops - 2 PDF

This document contains 5 multiple choice questions about loops in Java. The questions test understanding of for loops, nested for loops, and while loops. Each question includes 4 possible answer choices and the correct answer is provided at the end.

Uploaded by

Ashish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Problems on loops-2

MCQs
MCQs

Q1. Which of the following code segments will produce the displayed output?
1
22
333
4444
55555

I. for (int i = 1; i <= 5; i++) {


for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}

II. for (int i = 0; i < 5; i++) {


for (int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}

III. for (int i = 1; i < 5; i++) {


for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}

IV. for (int i = 0; i < 5; i++) {


for (int j = 0; j < i; j++) {
System.out.print(i+1);
}
System.out.println();
}

A. I B. II
C. III D. IV

Cracking the Coding Interview in JAVA - Foundation


MCQs

Q2. Which of the following code segments will produce the displayed output?
11111
2222
333
44
5

I. for (int j = 1; j <= 5; j++) {


for (int k = 5; k >= j; k--) {
System.out.print(j + " ");
}
System.out.println();
}

II. for (int j = 1; j <= 5; j++) {


for (int k = 5; k >= 1; k--) {
System.out.print(j + " ");
}
System.out.println();
}

III. for (int j = 1; j <= 5; j++) {


for (int k = 1; k <= j; k++) {
System.out.print(j + " ");
}
System.out.println();
}

IV. for (int j = 1; j <= 5; j++) {


for (int k = 1; k <= 5; k++) {
System.out.println(j + " ");
}
}

A. I B. II
C. III D. IV

Cracking the Coding Interview in JAVA - Foundation


MCQs

Q3. How many stars are output when the following code is executed?
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
System.out.println("*");
}

A. 10 B. 5
C. 25 D. 50

Q4. What is printed as a result of the following code segment?


for (int k = 0; k < 20; k+=2) {
if (k % 3 == 1)
System.out.println(k + " ");
}

A. 0 2 4 6 8 10 12 14 16 18 B. 4 16
C. 0 6 12 18 D. 4 10 16

Q5. What are the values of var1 and var2 after the following code segment is executed and the while
loop finishes?
int var1 = 0;
int var2 = 2;

while ((var2 != 0) && ((var1 / var2) >= 0)) {


var1 = var1 + 1;
var2 = var2 - 1;
}

A. var1 = 0, var2 = 2 B. var1 = 1, var2 = 1


C. var1 = 3, var2 = -1 D. var1 = 2, var2 = 0

Answers:
Ans 1: A. I
Ans 2: A. I
Ans 3: C. 25
Ans 4: D. 4 10 16
Ans 5: D. var1 = 2, var2 = 0

Cracking the Coding Interview in JAVA - Foundation

You might also like