4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
Computer Science & Information Technology
JAVA with OOPS
DPP: 1
Fundamentals of Java
Q1 What is the output of the following code? (C) Multi-line comment spanning multiple lines.
public class CosmicBanana { (D) Compilation error
public static void main(String[] args) {
Q4 What is the output of the following code?
int a = 2, b = 3, c = 4;
public class FluffyCloud {
System.out.println(a + b * c - b / a);
public static void main(String[] args) {
}
int x = 5;
}
double y = 2.0;
(A) 10 (B) 11
System.out.println(x / y);
(C) 13 (D) 14
}
Q2 What is the output of the following code? }
public class WittyButterfly { (A) 2 (B) 2.0
public static void main(String[] args) { (C) 2.5 (D) 2.50
String[] fruits = {"apple", "banana",
Q5 What is printed by the following code?
"cherry"};
public class MistyCave {
for (String fruit : fruits) {
public static void main(String[] args) {
System.out.print(fruit.charAt(0) + " ");
int i = 10;
}
double d = i;
}
System.out.println(d);
}
}
(A) a b c
}
(B) 97 98 99
(A) 10 (B) 10.0
(C) apple banana cherry
(C) 0.0 (D) 10.00
(D) A B C
Q6 What is the output of the code below?
Q3 What will the following code print?
public class WackyTurtle {
public class WhisperingRock {
public static void main(String[] args) {
public static void main(String[] args) {
int x = 10, y = 3;
// This is a single-line comment
System.out.println(x + y);
/*
System.out.println(x - y);
Multi-line comment
System.out.println(x * y);
spanning multiple lines.
System.out.println(x / y);
*/
System.out.println(x % y);
System.out.println("Comments in Java");
}
}
}
}
(A) 13, 7, 30, 3, 1
(A) Comments in Java
(B) 13, 7, 30, 3, 0
(B) // This is a single-line comment
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 1/7
4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
(C) 13, 7, 30, 3, 2 (C) 1 (D) 0
(D) 13, 7, 30, 4, 1
Q11 What is the output?
Q7 What is the printed value? public class MightyMongoose {
public class GrumpyParrot { public static void main(String[] args) {
public static void main(String[] args) { int num = 8;
int a = 10; if (num % 2 == 0) {
a += 5; System.out.println("Even");
a *= 2; } else {
System.out.println(a); System.out.println("Odd");
} }
} }
(A) 20 (B) 25 }
(C) 30 (D) 35 (A) Even
(B) Odd
Q8 What will be the output of the following code?
(C) 8 is even
public class MysticSquirrel {
(D) Compilation error
public static void main(String[] args) {
String s = "Hello"; Q12 What will be printed?
s += " World"; public class SecretOctopus {
System.out.println(s); public static void main(String[] args) {
} String color = "green";
} switch(color) {
(A) HelloWorld (B) Hello World case "red": System.out.println("Stop"); break;
(C) Hello (D) World Hello case "green": System.out.println("Go");
break;
Q9 What is the approximate output?
default: System.out.println("Unknown");
public class CosmicLlama {
}
public static void main(String[] args) {
}
double r = 3.5;
}
double area = Math.PI * r * r;
(A) Stop (B) Go
System.out.println(area);
(C) Unknown (D) No output
}
} Q13 What is the output?
(A) 38.48 (B) 38.5 public class DizzyOstrich {
(C) 38.00 (D) 39.0 public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
Q10 What does the following code print?
System.out.print(i + " ");
public class InvisiblePenguin {
}
public static void main(String[] args) {
}
boolean isTrue = (5 > 3);
}
System.out.println(isTrue);
(A) 0 1 2 3 (B) 1 2 3 4 5
}
(C) 0 1 2 3 4 (D) 0 1 2 3 4 5
}
(A) false (B) true Q14
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 2/7
4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
What does the following code print? System.out.print(i + " ");
public class CuriousNarwhal { }
public static void main(String[] args) { }
int i = 0; }
while (i < 3) { (A) 0 1 2 (B) 0 1 2 3
System.out.print(i + " "); (C) 0 1 2 3 4 (D) 1 2 3
i++;
Q18 What is printed by the following nested loops?
}
public class EccentricPanda {
}
public static void main(String[] args) {
}
for (int i = 1; i <= 2; i++) {
(A) 0 1 2 (B) 0 1 2 3
for (int j = 1; j <= 2; j++) {
(C) 1 2 3 (D) 0 1 2 3 4
System.out.print(i * j + " ");
Q15 What is the output of this code? }
public class SneakyLobster { System.out.println();
public static void main(String[] args) { }
int i = 3; }
do { }
System.out.print(i + " "); (A) 1 2 (B) 1 1
i++; 24 22
} while(i < 3); (C) 1 2 (D) 1 2
} 34 34
}
Q19 What is the output of the following code?
(A) No output
public class MysteryKangaroo {
(B) 3 printed once
static int party = 100;
(C) 3 printed twice
public static void main(String[] args) {
(D) Infinite loop
int party = 50;
Q16 What does the following code print? {
public class HazyPlatypus { int confusion = 25;
public static void main(String[] args) { System.out.println(confusion);
for (int i = 0; i < 5; i++) { }
if (i == 2) continue; System.out.println(party);
System.out.print(i + " "); System.out.println(MysteryKangaroo.party);
} }
} }
} (A) 25, 50, 100
(A) 0 1 2 3 4 (B) 0 1 3 4 (B) 25, 25, 25
(C) 0 2 3 4 (D) 0 1 2 4 (C) 25, 100, 50
(D) Compilation error
Q17 What is the output?
public class JollyZebra { Q20 What does the following code print?
public static void main(String[] args) { public class VividPenguin {
for (int i = 0; i < 5; i++) { public static void main(String[] args) {
if (i == 3) break; int[] numbers = {2, 4, 6, 8};
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 3/7
4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
System.out.println(numbers[2]); System.out.println(b);
} }
} }
(A) 2 (B) 4 (A) 10 (B) 12
(C) 6 (D) 8 (C) 13 (D) 11
Q21 What is the output of the following code? Q24 What does the following code print?
public class BaffledKoala { public class QuirkyRhino {
public static void main(String[] args) { public static void main(String[] args) {
int[] values = {1, 2, 3, 4}; int sum = 0;
values[1] = values[1] + 5; for (int i = 1; i <= 10; i++) {
System.out.println(values[1]); if (i % 2 == 0)
} sum += i;
} }
(A) 2 (B) 5 System.out.println(sum);
(C) 7 (D) 6 }
}
Q22 What is printed by this code?
(A) 20 (B) 30
public class ObscureEagle {
(C) 40 (D) 50
public static void main(String[] args) {
double d = 7.8; Q25 What is the output of the following code?
int i = (int) d; public class QuirkyUnicorn25 {
System.out.println(i); public static void main(String[] args) {
} int[] numbers = new int[3];
} for (int i = 0; i < numbers.length; i++) {
(A) 7 (B) 8 numbers[i] = i + 2;
(C) 7.8 (D) 0 }
System.out.println(numbers[0] + " " +
Q23 What is the output?
numbers[1] + " " + numbers[2]);
public class ConfusedWalrus {
}
public static void main(String[] args) {
}
int a = 5;
(A) 0 1 2 (B) 1 2 3
int b = a++ + ++a;
(C) 2 3 4 (D) 3 4 5
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 4/7
4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
Answer Key
Q1 (C) Q14 (A)
Q2 (A) Q15 (B)
Q3 (A) Q16 (B)
Q4 (C) Q17 (A)
Q5 (B) Q18 (A)
Q6 (A) Q19 (A)
Q7 (C) Q20 (C)
Q8 (B) Q21 (C)
Q9 (A) Q22 (A)
Q10 (B) Q23 (B)
Q11 (A) Q24 (B)
Q12 (B) Q25 (C)
Q13 (C)
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 5/7
4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
Hints & Solutions
Q1 Text Solution: The loop iterates with i from 0 to 4, printing these five
Multiplication and division occur before addition and values.
subtraction. Here, 3 × 4 = 12 and 3/2 = 1 (integer
Q14 Text Solution:
division), then 2 + 12 – 1 = 13. The while loop prints values 0, 1, and 2 because it
Q2 Text Solution: continues while i < 3.
The charAt(0) method returns the first character of Q15 Text Solution:
each string, and concatenation with " " converts it into
The do-while loop executes once before checking the
a string. Hence, the output is "a b c".
condition, printing 3 one time.
Q3 Text Solution:
Q16 Text Solution:
The compiler ignores all comments, so only the print
When i equals 2, the loop skips printing that iteration,
statement executes. printing 0, 1, 3, and 4.
Q4 Text Solution: Q17 Text Solution:
Dividing an integer by a double triggers implicit The loop exits when i equals 3, printing only 0, 1, and
casting, resulting in 5/2.0 = 2.5. 2.
Q5 Text Solution: Q18 Text Solution:
The int 10 is automatically cast to a double, so the
For i=1, prints 1×1 and 1×2; for i=2, prints 2×1 and
output is 10.0. 2×2.
Q6 Text Solution: Q19 Text Solution:
The operations yield: 10+3=13; 10-3=7; 10×3=30; The inner block variable is 25, the local variable in
10/3=3 (integer division); 10%3=1. main is 50, and the class variable is 100.
Q7 Text Solution:
Q20 Text Solution:
After a += 5, a becomes 15; then 15 multiplied by 2 Arrays are zero-indexed; numbers[2] is the third
equals 30.
element (6).
Q8 Text Solution:
Q21 Text Solution:
The += operator concatenates " World" to "Hello",
The second element (2) is increased by 5, resulting in
resulting in "Hello World".
7.
Q9 Text Solution:
Q22 Text Solution:
With π≈3.1416, the area ≈ 3.1416×3.5×3.5 ≈ 38.48. Casting a double to an int truncates the decimal part,
Q10 Text Solution: so 7.8 becomes 7.
Since 5 is greater than 3, the boolean expression
Q23 Text Solution:
evaluates to true.
a++ returns 5 (then a becomes 6); ++a increments a to
Q11 Text Solution: 7; b = 5 + 7 = 12.
8 is divisible by 2, so the code prints "Even".
Q24 Text Solution:
Q12 Text Solution: The even numbers 2, 4, 6, 8, 10 add up to 30.
The case for "green" matches the variable, so "Go" is
Q25 Text Solution:
printed. The loop assigns each element as (index + 2). Thus,
Q13 Text Solution: numbers[0]=2, numbers[1]=3, numbers[2]=4, and the
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 6/7
4/10/25, 11:43 PM GATE_Computer Science & Information Technology _DPP 1
GATE
output is "2 3 4".
Android App | iOS App | PW Website
https://qbg-admin.penpencil.co/finalize-question-paper/print-preview 7/7