wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Comp 2522 MT Review BBY Part 1

Total questions: 91

Worksheet time: 45mins

Name
Class
Date
1.

Which of the following is an access modifier in Java?

a)

Static

b)

Final

c)

Private

d)

String

2.

What is the purpose of using Javadocs in Java?

a)

To compile Java code

b)

To document Java code

c)

To execute Java code

d)

To debug Java code

3.

Which of the following is a primitive type in Java?

a)

String

b)

Integer

c)

int

d)

ArrayList

4.

What is constructor chaining in Java?

a)

Calling one constructor from another

b)

Creating multiple constructors in a class

c)

Overloading constructors

d)

Using constructors to initialize variables

5.

Which of the following is a type in Java used to define a blueprint for creating objects?

a)

Interface

b)

Record

c)

Class

d)

Annotation

6.

What Java type is used to define a group of constants?

a)

Enum

b)

Class

c)

Interface

d)

Annotation

7.

Which Java type is primarily used to define methods that must be implemented by a class?

a)

Record

b)

Interface

c)

Annotation

d)

Enum

8.

What is the purpose of an annotation in Java?

a)

To create a new data type

b)

To provide metadata about the code

c)

To define a group of constants

d)

To implement multiple inheritance

9.

Which Java type is used to model immutable data?

a)

Class

b)

Interface

c)

Record

d)

Enum

10.

What is a Java class used to describe?

a)

Only the data of a category

b)

Only the behaviors of a category

c)

Both the data and behaviors of a category

d)

The user interface of a program

11.

What are instance variables also known as?

a)

Methods

b)

Functions

c)

Procedures

d)

Fields

12.

What is the purpose of a constructor in a Java class?

a)

To define the class name

b)

To initialize an instance of a class

c)

To delete an instance of a class

d)

To compile the class

13.

How should a class name be formatted in Java?

a)

All lowercase letters

b)

All uppercase letters

c)

CamelCase starting with an uppercase letter

d)

Snake_case with underscores

14.

What is an object in the context of programming?

a)

A type of data structure

b)

One instance of a class

c)

A function within a program

d)

A variable that stores data

15.

Which keyword is used to create an object of a class in programming?

a)

create

b)

object

c)

new

d)

instance

16.

In the code snippet provided, what does 'b1' represent?

a)

A method

b)

A class

c)

A reference to a BankAccount object

d)

A variable

17.

What is the recommended way to declare a BankAccount variable according to the convention?

a)

BankAccount b1 = new BankAccount();

b)

final BankAccount b1;

c)

BankAccount b1;

d)

BankAccount b1 = new BankAccount(100.00);

18.

Why is it beneficial to declare all data as final, with some exceptions, in programming?

a)

It makes the code run faster.

b)

It allows for more flexible code changes.

c)

It results in code that is organized, fast, simple to read, and easy to debug.

d)

It increases the complexity of the code.

19.

Which of the following is a correct way to "initialize" a BankAccount with a $100 initial balance?

a)

b1 = new BankAccount();

b)

b1 = new BankAccount(100.00);

c)

final BankAccount b1 = new BankAccount();

d)

BankAccount b1;

20.

What is the purpose of grouping variable declarations and initializations together?

a)

To make the code more complex.

b)

To allow for easier debugging and reading.

c)

To increase the execution time of the code.

d)

To make the code less organized.

21.

What must be explicitly declared when creating a Java variable?

a)

The variable's name

b)

The variable's datatype

c)

The variable's value

d)

The variable's memory address

22.

What is the main difference between primitive types and reference types in Java?

a)

Primitive types hold a memory address, reference types hold a value

b)

Primitive types are complex, reference types are simple

c)

Primitive types hold a value, reference types hold a memory address

d)

Primitive types are always integers, reference types are always strings

23.

What is the data type used by Java for a single Unicode character in single quotation marks?

a)

String

b)

char

c)

int

d)

double

24.

How does Java treat Unicode characters in double quotation marks?

a)

char

b)

int

c)

String

d)

boolean

25.

What is the purpose of an array in Java?

a)

To store a single value

b)

To store a fixed number of values of a single datatype

c)

To store multiple data types

d)

To perform calculations

26.

What is the default value for an `int` type in Java?

a)

0

b)

1

c)

-1

d)

null

27.

What is the default value for a `char` type in Java?

a)

'A'

b)

'\u0000'

c)

' ' (space)

d)

null

28.

In Java, what is the default value for a `String` type?

a)

"" (empty string)

b)

null

c)

"default"

d)

" " (space)

29.

What is the default value for an `array` type in Java?

a)

[] (empty array)

b)

null

c)

[0]

d)

[null]

30.

What is the primary difference between 'null' and an empty string ("") in programming?

a)

'null' means no object, while "" is a string with no characters.

b)

'null' and "" are the same and interchangeable.

c)

'null' has methods available, while "" does not.

d)

Both 'null' and "" have a length of 0.

31.

Why should strings almost always be initialized to 'null' rather than an empty string ("")?

a)

To ensure the string has methods available.

b)

To avoid confusion between no object and an empty object.

c)

To make sure the string has a length of 0.

d)

To ensure the string is located at an address.

32.

Which of the following statements is true about reference types in programming?

a)

Only primitive types can be null.

b)

Reference types cannot be null.

c)

Any reference type can be null, indicating no object.

d)

Reference types are always initialized to "".

33.

What will happen when you try to execute `System.out.println(s1.length());` if `s1` is declared as `final String s1 = null;`?

a)

It will print 0.

b)

It will print null.

c)

It will throw a NullPointerException.

d)

It will print an empty string.

34.

What is the output of `System.out.println(s2.length());` if `s2` is declared as `final String s2 = "";`?

a)

0

b)

NullPointerException

c)

1

d)

null

35.

What does the method `s.isEmpty()` return true for?

a)

When the string is null

b)

When the string is not null, but is empty

c)

When the string contains only whitespace

d)

When the string is not empty

36.

Which method checks if a string is not null and contains only whitespace?

a)

s.isEmpty()

b)

s.isBlank()

c)

s.isNull()

d)

s.isWhitespace()

37.

Why is the check `if(s != null && !s.isEmpty())` considered a good practice?

a)

It crashes if the string is null

b)

It ensures the string is not null before checking if it is empty

c)

It checks for whitespace in the string

d)

It executes faster than other checks

38.

What is the potential issue with the check `if(!s.isEmpty() && s != null)`?

a)

It will not execute if the string is empty

b)

It crashes if the string is null

c)

It checks for null before checking if the string is empty

d)

It is slower than other checks

39.

Why should instance variables be private in a class?

a)

To allow other classes to alter their values directly

b)

To prevent other classes from directly altering their values

c)

To make them accessible to all classes

d)

To ensure they are always static

40.

What is the recommended practice for instance variables that are not expected to change?

a)

Make them public

b)

Make them static

c)

Make them final

d)

Make them protected

41.

What should you do if a variable belongs to a class?

a)

Make it public

b)

Make it static

c)

Make it private

d)

Make it final

42.

What principle suggests giving the smallest possible access to your data?

a)

Principle of Maximum Access

b)

Principle of Least Privilege

c)

Principle of Open Access

d)

Principle of Data Sharing

43.

Why is it important to include units in variable names?

a)

To make the code look longer

b)

To ensure clarity and prevent errors

c)

To confuse other programmers

d)

To reduce the number of variables

44.

In a program that stores user information, which of the following is the best example of a descriptive and well-named variable?

a)

x

b)

userBirthDate

c)

data1

d)

tempVar

45.

What is the entry point of a Java program where it begins execution?

a)

The constructor method

b)

The main method

c)

The print method

d)

The static method

46.

In the Java main method, what must the arguments be declared as (from Jason's lecture)?

a)

public

b)

static

c)

final

d)

private

47.

What does the keyword 'final' mean in Java?

a)

A variable can be changed multiple times

b)

A variable cannot change its value after it's set once

c)

A variable is automatically initialized

d)

A variable is only accessible within its class

48.

In Java, how should variables be declared and initialized according to the conventions?

a)

Declare and initialize in the same line

b)

Declare in one line, initialize in a separate line

c)

Initialize first, then declare

d)

Declare and initialize in any order

49.

In Java, what keyword is used to make one class inherit another?

a)

inherit

b)

extends

c)

implements

d)

super

50.

Every Java class implicitly extends the Object class.

a)

true

b)

false

51.

If a class does not define any constructor, what happens?

a)

It cannot be instantiated

b)

Compilation fails

c)

It inherits constructors from the parent class

d)

Java automatically provides a default constructor

52.

The super() call must always be the first line in a constructor in all versions of Java.

a)

true

b)

false

53.

Why is it a bad idea to make instance variables protected directly?

a)

They become immutable

b)

They cannot be inherited

c)

They break encapsulation

d)

They can be accessed outside the package

54.

Using protected getters and setters is safer than making instance variables protected.

a)

true

b)

false

55.

What is polymorphism?

a)

Treating different child objects through the same parent type

b)

Writing multiple classes with the same name

c)

Converting data types

d)

Overloading constructors

56.

According to the Liskov Substitution Principle, a subclass should be usable anywhere its parent class is expected.

a)

true

b)

false

57.

If Animal a = new Dog();, what is the static type of a?

a)

Depends on runtime

b)

Object

c)

Dog

d)

Animal

58.

At runtime, overridden methods are chosen based on the object’s actual (dynamic) type.

a)

true

b)

false

59.

Which of the following is a checked exception?

a)

ArithmeticException

b)

ArrayIndexOutOfBoundsException

c)

IOException

d)

NullPointerException

60.

Checked exceptions must either be caught with try/catch or declared with throws.

a)

true

b)

false

61.

Unchecked exceptions (RuntimeExceptions) must always be caught, or the program won’t compile.

a)

true

b)

false

62.

What is the benefit of creating a custom exception class?

a)

It prevents all crashes

b)

It always improves performance

c)

It replaces all standard exceptions

d)

It makes error handling more readable and specific

63.

Private methods and static methods in Java can be overridden.

a)

true

b)

false

64.

Declaring a method as final prevents subclasses from overriding it.

a)

true

b)

false

65.

Why are setter methods often declared final in parent classes?

a)

To automatically generate getters

b)

To ensure only the parent class controls how its fields are changed

c)

To improve performance

d)

To allow child classes to add more rules

66.

A class declared as final cannot be extended.

a)

true

b)

false

67.

By default, the equals() method in Java’s Object class compares:

a)

Field values

b)

Types only

c)

Hash codes

d)

Object memory addresses

68.

If you override equals(), you don't have to override hashCode().

a)

true

b)

false

69.

Which rule must hold between equals() and hashCode()?

a)

Hash codes determine memory address

b)

Equal objects must have different hash codes

c)

Hash codes must always be unique

d)

Equal objects must have equal hash codes

70.

An abstract method has no body and must be implemented by a concrete subclass.

a)

true

b)

false

71.

Which declaration is correct for an abstract method?

a)

final abstract void move();

b)

public static void move();

c)

public void move();

d)

public abstract void move();

72.

Which of the following statements about abstract classes is correct?

a)

They cannot have fields

b)

They cannot contain constructors

c)

They cannot contain any concrete methods

d)

They cannot be instantiated

73.

If a class has even one abstract method, the class itself must be declared abstract.

a)

true

b)

false

74.

If a parent class has an abstract method, what must its child class do?

a)

Implement it or declare itself abstract

b)

Declare it final

c)

Override it as static

d)

Ignore it

75.

The opposite of an abstract class is a concrete class.

a)

true

b)

false

76.

Why would you use an abstract parent class with abstract methods?

a)

To guarantee that all subclasses provide an implementation

b)

To avoid using interfaces

c)

To reduce compilation time

d)

To make subclasses optional

77.

Which keyword is used for a class to inherit from an interface?

a)

interface

b)

extends

c)

inherits

d)

implements

78.

A Java class can implement multiple interfaces.

a)

true

b)

false

79.

Which of the following can an interface contain?

a)

Only abstract methods

b)

Instance variables

c)

Constructors

d)

Abstract methods, constants, and default methods

80.

Interfaces in Java can have constructors.

a)

true

b)

false

81.

Which is a key difference between abstract classes and interfaces?

a)

Abstract classes cannot contain constructors

b)

Abstract classes cannot have methods

c)

Interfaces allow multiple inheritance

d)

Interfaces cannot contain constants

82.

A class can extend one parent class but implement multiple interfaces.

a)

true

b)

false

83.

By default, all variables in an interface are:

a)

instance variables

b)

public static final

c)

private

d)

protected

84.

Constants declared in an interface must be initialized.

a)

true

b)

false

85.

Interfaces can provide method bodies if the method is marked default.

a)

true

b)

false

86.

Why were default methods introduced in interfaces?

a)
  • To prevent multiple inheritance

b)

To allow interfaces to have fields

c)

To replace abstract classes

d)

To allow backward compatibility when adding methods

87.

The Comparable interface is used for:

a)

Defining a natural ordering

b)

Checking object equality

c)

Comparing object references only

d)

Defining multiple sort orders

88.

Comparator allows us to define multiple custom orderings for objects.

a)

true

b)

false

89.

If a class implements Comparable, which method must it override?

a)

compare()

b)

compareTo()

c)

hashCode()

d)

equals()

90.

Which interface would you use if you need two different sorting logics for the same class?

a)

Iterable

b)

Cloneable

c)

Comparable only

d)

Comparator

91.

You can NOT use both Comparable and Comparator together in the same program.

a)

true

b)

false