Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Page 1

Total questions: 51

Worksheet time: 26mins

Name
Class
Date
1.

Which of the following declarations will NOT compile?

a)

double weight = 2.5;

b)

int items = 3;

c)

boolean open = false;

d)

int taxRate = 0.07;

2.

Which correctly declares and initializes a char variable named initial to the letter Q?

a)

char initial = "Q";

b)

char initial = 'Q';

c)

String initial = 'Q';

d)

char initial = Q;

3.

Which declaration correctly initializes a float variable named f to 2.5 without using a cast?

a)

float f = 2.5;

b)

float f = 2.5f;

c)

float f = 2.5d;

d)

double f = 2.5f;

4.

Given: int n = 10; double d = 4.2; Which statement does NOT compile?

a)

d = n;

b)

n = (int) d;

c)

n = d;

d)

d = (double) n;

5.

Which is a correct declaration of a boolean named isValid initialized to true?

a)

boolean isValid = true;

b)

Boolean isValid = "true";

c)

bool isValid = true;

d)

boolean isValid = 1;

6.

Which statement correctly creates a String variable named color with the value "blue"?

a)

String color = "blue";

b)

String color = 'blue';

c)

string color = "blue";

7.

Which declaration compiles correctly?

a)

long big = 3000000000;

b)

long big = 3000000000L;

c)

int big = 3000000000L;

d)

long big = "3000000000";

8.

Which statement correctly declares an int variable named level and initializes it to 7?

a)

int level = 7;

b)

int level = 7.0;

c)

double level = 7;

d)

String level = 7;

9.

Consider: int a = 10; int b = 5; a = b; b = a + 7; What is the value of b after execution?

a)

5

b)

12

c)

17

d)

10

10.

Consider: String status = "open"; status = "closed"; String msg = status; status = "pending"; What is the value of msg?

a)

"open"

b)

"closed"

c)

"pending"

d)

The code does not compile.

11.

Consider: boolean flag = false; flag = true; boolean copy = flag; flag = false; What is the value of copy?

a)

false

b)

null

c)

true

d)

The code does not compile.

12.

Consider: int x = 3; double y = 2.0; y = y + x; x = x * 2; What is the final value of y?

a)

2.0

b)

5.0

c)

6.0

d)

The code does not compile.

13.

Consider: int x = 4; x++; x = x * 2; What is the value of x?

a)

8

b)

9

c)

10

d)

12

14.

Consider: int total = 8; total -= 3; total += 2; What is the value of total?

a)

3

b)

7

15.

Consider: int a = 1; int b = 2; int temp = a; a = b; b = temp; What is the value of a?

a)

1

b)

2

c)

3

d)

0

16.

Consider: int p = 4; int q = p + 1; p = q * 2; q = p - q; What is the value of q?

a)

4

b)

5

c)

9

d)

10

17.

Consider: int x = 3; double y = 2.0; double z = 5.0; z = y * x - (14 % x + z); What is the value of z?

a)

-1.0

b)

1.0

c)

6.0

d)

7.0

18.

Consider the following code: int a = 7; int b = 2; double c = a / b; What is the value of c?

a)

3.0

b)

3.5

c)

0.0

d)

The code does not compile.

19.

Consider the following code: int a = 7; int b = 2; double c = (double) a / b; What is the value of c?

a)

3.0

b)

3.5

c)

4.0

d)

The code does not compile.

20.

Consider the following code: int n = 10; int d = 0; int q = n / d; What happens when this code runs?

a)

q becomes 0

b)

The code does not compile

c)

An ArithmeticException occurs at runtime

d)

q becomes Infinity

21.

Consider the following code: double n = 10.0; double d = 0.0; double q = n / d; System.out.println(q); What prints?

a)

Infinity

b)

0.0

c)

An ArithmeticException occurs

22.

Consider: int x = 5; int y = 2; int result = x + y * 3; What is result?

a)

21

b)

11

c)

9

d)

7

23.

Consider: int x = 5; int y = 2; int result = (x + y) * 3; What is result?

a)

21

b)

11

c)

9

d)

7

24.

Consider: int x = 17 % 5; int y = 2 + x * 3; What is the value of y?

a)

2

b)

5

c)

8

d)

11

25.

Which replacement correctly assigns the parameter to the instance variable?

a)

this.id = id;

b)

id = this.id;

c)

this.id = this.id;

d)

id = 0;

26.

Which class demonstrates constructor overloading? A. public class Pen { public Pen() { } } B. public class Pen { public void Pen(int x) { } } C. public class Pen { public Pen() { } public Pen(String color) { } } D. public class Pen { public Pen() { } public void setColor(String c) { } }

a)

A

b)

B

c)

C

d)

D

27.

Given: public class Book { public Book(String title, int pages) { } public Book(int pages) { } } Which line uses the one-parameter constructor?

a)

Book b = new Book(300);

b)

Book b = new Book("Dune", 412);

c)

Book b = new Book("Dune");

d)

Book b = new Book();

28.

Why does this class not compile? public class Timer { private int seconds; public Timer() { seconds = 0; this(30); } public Timer(int s) { seconds = s; } }

a)

this(30) must be the first statement in the constructor

b)

Constructors cannot call this(...)

29.

Consider: public class Timer { private int seconds; public Timer() { this(30); } public Timer(int s) { seconds = s; } } Timer t = new Timer(); What is seconds in t?

a)

0

b)

30

c)

Uninitialized

d)

Compile error

30.

Given: public class Person { private String name; public Person(String name) { this.name = name; } } public class Student extends Person { public Student(String name) { // missing line } } Which line should be added so the code compiles?

a)

Person(name);

b)

super(name);

c)

this(name);

d)

name = super.name;

31.

Which statement about super(...) in a constructor is true?

a)

It can appear anywhere in the constructor

b)

It must be the first statement if used

c)

It must appear in every method

d)

It cannot have arguments

32.

A class already has: public Car(int year) { } Which header correctly overloads that constructor?

a)

public Car(int year, String model) { }

b)

public void Car(int year, String model) { }

c)

public car(int year, String model) { }

d)

public Car(int year) { }

33.

Given: public class Account { private double balance; public Account(double b) { balance = b; } } Which method allows other classes to access but not modify balance?

a)

public void getBalance() { return balance; }

b)

private double getBalance() { return balance; }

c)

public void setBalance(double b) { balance = b; }

d)

public double getBalance() { return balance; }

34.

Given: public class Player { private String name; public void setName(String name) { // which line? } } Which line correctly updates the instance variable?

a)

name = this.name;

b)

this.name = name;

c)

String name = name;

d)

return name;

35.

Why doesn't this compile? public class Movie { private String title; public Movie(String t) { title = t; } public getTitle() { return title; } }

a)

title must be public

b)

getTitle must take a parameter

c)

getTitle is missing a return type in its header

d)

return is not allowed in methods

36.

Given: public class Average { private int sum; public int inputs; } Which statement is true?

a)

Both are visible outside the class

b)

sum is visible only within the class; inputs is visible outside the class

c)

sum is visible outside; inputs is visible only within the class

d)

Neither is visible anywhere

37.

Why doesn't this compile? public class Student { private double gpa; public Student(double gpa) { this.gpa = gpa; } public void setGpa(double newGpa) { gpa = newGpa; } } Student s = new Student(3.0); System.out.println("New GPA: " + s.setGpa(3.5));

a)

setGpa returns void and cannot be used in a string concatenation

b)

The constructor must have no parameters

c)

gpa is private so setGpa cannot modify it

d)

The literal 3.5 must be written as an int

38.

For private double price; which is a correct getter?

a)

public void getPrice() { return price; }

b)

public double getPrice(double newPrice) { return price; }

c)

public double getPrice() { return price; }

d)

private void getPrice() { return price; }

39.

Given: public class Gadget { private int id; public Gadget(int id) { this.id = id; } } Which line in another class causes a compile-time error?

a)

Gadget g = new Gadget(10);

b)

System.out.println(g.id);

c)

Gadget g2 = new Gadget(0);

d)

System.out.println("done");

40.

Given: public class Ticket { private int seatNumber; public Ticket(int seatNumber) { this.seatNumber = seatNumber; } } Which addition allows other classes to know the seat number but prevents them from changing it?

a)

public void setSeatNumber(int n) { seatNumber = n; }

b)

public int getSeatNumber() { return seatNumber; }

c)

public int seatNumber() { return 0; }

d)

public int getSeatNumber(int n) { seatNumber = n; return seatNumber; }

41.

Consider: public class Animal { private String type; public Animal(String type) { this.type = type; } } public class Dog extends Animal { private int age; public Dog(String type, int age) { super(type); this.age = age; } } Dog d = new Dog("mammal", 4); Which best describes the effect?

a)

Dog cannot call super because type is private

b)

Only age is initialized

c)

super(type) makes type public in Dog

d)

Animal initializes type to "mammal", then Dog initializes age to 4

42.

Why does Tree not compile? public class Plant { private String leafColor; public Plant(String leafColor) { this.leafColor = leafColor; } } public class Tree extends Plant { public Tree(String leafColor) { super(leafColor); } public void repaint(String c) { leafColor = c; } }

a)

The repaint method attempts to access the private field leafColor declared in Plant

b)

The Tree constructor fails to call super(leafColor) as its first statement

c)

The repaint method must be static to modify leafColor

d)

The class Tree must declare a no-argument constructor to compile

43.

Select the correct statement.

a)

leafColor is private in Plant, so Tree can’t access it directly

b)

repaint must be a constructor

c)

super(leafColor) must be the last statement in the constructor

d)

The parameter c must be an int

44.

Consider: public class Plant { private String leafColor; public Plant(String leafColor) { this.leafColor = leafColor; } public void setLeafColor(String c) { leafColor = c; } } public class Tree extends Plant { public Tree(String leafColor) { super(leafColor); } } Tree t = new Tree("green"); t.setLeafColor("red"); What happens?

a)

Does not compile because leafColor is private

b)

Compiles; the leafColor inside the object becomes "red"

c)

Compiles but does not change leafColor because it’s private

d)

Throws an exception

45.

Given: public class Shape { public double area() { return 0.0; } } public class Square extends Shape { // choose one method } Which method in Square correctly overrides area()?

a)

public double area() { return 25.0; }

b)

public int area() { return 25; }

c)

public double area(int side) { return side * side; }

46.

Given: public class Appliance { public Appliance(String brand) { } } public class Toaster extends Appliance { public Toaster() { // no code here } } Which change inside Toaster() makes the code compile?

a)

Add super();

b)

Add super("Generic");

c)

Add this("Generic");

d)

Add Appliance("Generic");

47.

Given: public class Parent { public void greet() { System.out.print("Hi"); } } public class Child extends Parent { public void greet() { // want to call Parent's greet(), then print "!" } } Which line should appear first inside Child’s greet()?

a)

parent.greet();

b)

super.greet();

c)

this.greet();

d)

greet.super();

48.

Which prints exactly: Path: C:\temp\notes

a)

System.out.println("Path: C:\\temp\\notes");

b)

System.out.println("Path: C:\\\\temp\\\\notes");

c)

System.out.println("Path: C:/temp/notes");

d)

System.out.println("Path: C:\\\\temp\\\\\notes");

49.

Which prints exactly: You say "yes". I say "no".

a)

System.out.println("You say \"yes\".\nI say \"no\".");

b)

System.out.println("You say "yes".\nI say "no".");

c)

System.out.print("You say \"yes\"."); System.out.print("I say \"no\".");

d)

System.out.println("You say \"yes\".\nI say \"no\".")

50.

What is the output? System.out.print("A"); System.out.println("B"); System.out.print("C"); System.out.println("D");

a)

AB CD

b)

A B C D

c)

ABCD

d)

A BCD

51.

What is the output? System.out.println("A"); System.out.print("B"); System.out.println("C");

a)

A B C

b)

A BC

c)

ABC

d)

AB C