0% found this document useful (0 votes)
17 views10 pages

MOCK TEST DAY1 Fundamentals

The document contains a series of programming questions related to Java, covering topics such as data types, compilation errors, output of code snippets, and control flow structures. Each question presents multiple-choice answers that test the reader's understanding of Java syntax and behavior. The questions range from basic to more complex scenarios involving string comparison, arithmetic operations, and loop iterations.

Uploaded by

jainsparsh15jan
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)
17 views10 pages

MOCK TEST DAY1 Fundamentals

The document contains a series of programming questions related to Java, covering topics such as data types, compilation errors, output of code snippets, and control flow structures. Each question presents multiple-choice answers that test the reader's understanding of Java syntax and behavior. The questions range from basic to more complex scenarios involving string comparison, arithmetic operations, and loop iterations.

Uploaded by

jainsparsh15jan
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/ 10

1. What data type (or types) will allow the following code snippet to compile?

(Choose all that apply)

byte x = 5;
byte y = 10;
_____ z = x + y;

A. int
B. long
C. boolean
D. double
E. short
F. byte

2. Given that a Date class exists in both the java.util and java.sql packages, what
is the result of compiling the following class?

1: import java.util.*;
2: import java.sql.*;
3: public class BirthdayManager {
4: private Date rob = new Date(12/3/20);
5: private java.util.Date sharon =new java.util.Date();
6: }

A. The code does not compile because of lines 1 and 2.


B. The code does not compile because of line 4.
C. The code does not compile because of line 5.
D. The code compiles without issue.

3. What is the output of the following code snippet?

3: int x = 4;
4: long y = x * 4 - x++;
5: if(y<10) System.out.println("Too Low");
6: else System.out.println("Just right");
7: else System.out.println("Too High");

A. Too Low
B. Just Right
C. Too High
D. Compiles but throws a NullPointerException.
E. The code will not compile because of line 6.
F. The code will not compile because of line 7.
4. What is the output of the following code snippet?

3: int y = 1;
4: do{
5: System.out.print(y++ + " ");
6: } while(y == 10);

A. 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1
D. The code will not compile because of line 6.
E. The code contains an infinite loop and does not terminate.

5. What is the result of compiling and executing the following class?

1: public class Test {


2: int x = 234;
3: public static void main(String[] data) {
4: int y = 564;
5: System.out.print(x+y);
6: }
7: }

A. It does not compile.


B. It compiles but throws an exception at runtime.
C. It compiles and outputs 234.
D. It compiles and outputs 564.
E. It compiles and outputs 798.

6. Which of the following does not compile?

A. int num = 999;


B. int num = 9_9_9;
D. int $_ = 9;
C. int num = _9_99;
E. None of the above; they all compile.

7. Which is the first line to trigger a compiler error?


double d1 = 5f; // p1
double d2 = 5.0; // p2
float f1 = 5f; // p3
float f2 = 5.0; // p4

A. p1
B. p2
C. p3
D. p4

8. What is the output of the following application?

package dinosaur;
public class Park {
public final static void main(String... arguments) {

int pterodactyl = 3;
long triceratops = 5;
if(pterodactyl % 3 >= 1)
triceratops++;
triceratops--;
System.out.print(triceratops);
}

A. 2
B. 3
C. 4
D.5
E. The code does not compile.

9. What is the output of the following application?

package schedule;
public class PrintWeek {
public static final void main(String[] days) {
System.out.print(5+ “6”+ "7" + 8 + 9);
}
}

A. 56789
B. 11789
C. 51317
D. The code does not compile.

10. How many lines does the following code output?


String[] days = new String[] { "Sunday", "Monday",
"Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" };
for (int i = 1; i <days.length; i++)
System.out.println(days[i]);

A. Six
B. Seven
C. The code does not compile.
D. The code compiles but throws an exception at runtime.

11. Given
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1==s2);
System.out.println(s1==s3);

String name = "Hello World";


String name2 = new String("Hello World").intern();
System.out.println(name == name2);

What is output ?

A. true false
B. false true
C. true false true
D. false true false
E.Code will not compile

12. Given

int init = 11;


int split = 3;
int partA = init / split;
int partB = init % split;
int result = partA + split * partB ;
System.out.print(result+"\t");

What is the result?

A. 9
B. 11
C. 12
D. 15
E. The code does not compile.
F. None of the above.

13. Which is the first line containing a compiler error?

var title = "Weather"; // line x1


var hot = 100, var cold = 20; // line x2
var f = 32, int c = 0; // line x3

A. x1
B. x2
C. x3
D. None of the above

14. Given

String x1 = "Java";
StringBuilder x2 = new StringBuilder("Java");
String x3 = "Java";
System.out.println(x1==x2);
System.out.println(x1==x3);

What is output ?
A. true true
B. false false
C. true false
D. false true
E. Code will not compile.

15. What is the result of the following code?

var sb = new
StringBuilder("radical").insert(sb.length(), "robots");
System.out.println(sb);

A. radicarobots
B. radicalrobots
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
16.

What is the output of the following?


var teams = new StringBuilder ("333") ;
teams. append (" 806") ;
teams.append (" 1601") ;
System.out.print (teams) ;
A. 333
B. 333 806 1601
C. The code compiles but outputs something else.
D. The code does not compile.
A. Option A
B. Option B
C. Option C
D. Option D
E. Options E

17.
Given the file Test.java shown, which of the marked lines can you independently
insert the line var x; into and still have the code compile?

// line 1
public class Test{
// line 2
public void attach() {
// line 3
}
// line 4
}
A. line 2
B. line 3
C. line 2 and line 3
D. line 1, line 2,line 3 and line 4
E. None of the above

18.

Which of the following iterates a different number of times than the others?
A. for (int k=0; k < 5; k++) {}
B.
C.
for (int k=1; k <= 5; k++) {}
int k=0; do { } while (k++ < 5);
D. int k=0; while (k++ < 5) {}
E. All of these iterate the same number of times.
A. Option A
B. Option B
C. Option C
D. Option D
E. Options E

19.
The code contains six pairs of curly braces. How many pairs can be
removed without chang- ing the behavior?
12: public static void main(String[] args) {
13:
int secret = 0;
for (int i = 0; i < 10; i++) {
while (i < 10) {
if (i == 5) {
} else {
System.out.println("if");
System.out.println("in");
System.out.println("else");
14:
15:
16:
17:
18:
19:
20:
21:
}
22:
}
23:
}
24:
switch (secret) {
25:
case 0:
26:
}
27: }
System.out.println("zero");

A. One
B. Two
C. Three
D. Four
E. Five
F. Six

20.
Which of these code snippets behaves differently from the others?
A.
if (numChipmunks == 1)
System.out.println("One chipmunk");
if (numChipmunks == 2)
System.out.println("Two chipmunks");
if (numChipmunks == 3)
B.
System.out.println("Three chipmunks");
switch (numChipmunks) {
System.out.println("One chipmunk"); System.out.println("Two chipmunks");
case 3: System.out.println("Three chipmunks");
}
C.
if (numChipmunks
== 1)
System.out.println("One chipmunk");
else if (numChipmunks
==
2)
System.out.println("Two chipmunks");
else if (numChipmunks == 3)
System.out.println("Three chipmunks");
D. All three code snippets do the same thing.
A. Option A
B. Option B
C. Option C
D. Option D

You might also like