Font size
WorksheetsComp 2522 MT Review BBY Part 1
Total questions: 91
Worksheet time: 45mins
Which of the following is an access modifier in Java?
Static
Final
Private
String
What is the purpose of using Javadocs in Java?
To compile Java code
To document Java code
To execute Java code
To debug Java code
Which of the following is a primitive type in Java?
String
Integer
int
ArrayList
What is constructor chaining in Java?
Calling one constructor from another
Creating multiple constructors in a class
Overloading constructors
Using constructors to initialize variables
Which of the following is a type in Java used to define a blueprint for creating objects?
Interface
Record
Class
Annotation
What Java type is used to define a group of constants?
Enum
Class
Interface
Annotation
Which Java type is primarily used to define methods that must be implemented by a class?
Record
Interface
Annotation
Enum
What is the purpose of an annotation in Java?
To create a new data type
To provide metadata about the code
To define a group of constants
To implement multiple inheritance
Which Java type is used to model immutable data?
Class
Interface
Record
Enum
What is a Java class used to describe?
Only the data of a category
Only the behaviors of a category
Both the data and behaviors of a category
The user interface of a program
What are instance variables also known as?
Methods
Functions
Procedures
Fields
What is the purpose of a constructor in a Java class?
To define the class name
To initialize an instance of a class
To delete an instance of a class
To compile the class
How should a class name be formatted in Java?
All lowercase letters
All uppercase letters
CamelCase starting with an uppercase letter
Snake_case with underscores
What is an object in the context of programming?
A type of data structure
One instance of a class
A function within a program
A variable that stores data
Which keyword is used to create an object of a class in programming?
create
object
new
instance
In the code snippet provided, what does 'b1' represent?
A method
A class
A reference to a BankAccount object
A variable
What is the recommended way to declare a BankAccount variable according to the convention?
BankAccount b1 = new BankAccount();
final BankAccount b1;
BankAccount b1;
BankAccount b1 = new BankAccount(100.00);
Why is it beneficial to declare all data as final, with some exceptions, in programming?
It makes the code run faster.
It allows for more flexible code changes.
It results in code that is organized, fast, simple to read, and easy to debug.
It increases the complexity of the code.
Which of the following is a correct way to "initialize" a BankAccount with a $100 initial balance?
b1 = new BankAccount();
b1 = new BankAccount(100.00);
final BankAccount b1 = new BankAccount();
BankAccount b1;
What is the purpose of grouping variable declarations and initializations together?
To make the code more complex.
To allow for easier debugging and reading.
To increase the execution time of the code.
To make the code less organized.
What must be explicitly declared when creating a Java variable?
The variable's name
The variable's datatype
The variable's value
The variable's memory address
What is the main difference between primitive types and reference types in Java?
Primitive types hold a memory address, reference types hold a value
Primitive types are complex, reference types are simple
Primitive types hold a value, reference types hold a memory address
Primitive types are always integers, reference types are always strings
What is the data type used by Java for a single Unicode character in single quotation marks?
String
char
int
double
How does Java treat Unicode characters in double quotation marks?
char
int
String
boolean
What is the purpose of an array in Java?
To store a single value
To store a fixed number of values of a single datatype
To store multiple data types
To perform calculations
What is the default value for an `int` type in Java?
0
1
-1
null
What is the default value for a `char` type in Java?
'A'
'\u0000'
' ' (space)
null
In Java, what is the default value for a `String` type?
"" (empty string)
null
"default"
" " (space)
What is the default value for an `array` type in Java?
[] (empty array)
null
[0]
[null]
What is the primary difference between 'null' and an empty string ("") in programming?
'null' means no object, while "" is a string with no characters.
'null' and "" are the same and interchangeable.
'null' has methods available, while "" does not.
Both 'null' and "" have a length of 0.
Why should strings almost always be initialized to 'null' rather than an empty string ("")?
To ensure the string has methods available.
To avoid confusion between no object and an empty object.
To make sure the string has a length of 0.
To ensure the string is located at an address.
Which of the following statements is true about reference types in programming?
Only primitive types can be null.
Reference types cannot be null.
Any reference type can be null, indicating no object.
Reference types are always initialized to "".
What will happen when you try to execute `System.out.println(s1.length());` if `s1` is declared as `final String s1 = null;`?
It will print 0.
It will print null.
It will throw a NullPointerException.
It will print an empty string.
What is the output of `System.out.println(s2.length());` if `s2` is declared as `final String s2 = "";`?
0
NullPointerException
1
null
What does the method `s.isEmpty()` return true for?
When the string is null
When the string is not null, but is empty
When the string contains only whitespace
When the string is not empty
Which method checks if a string is not null and contains only whitespace?
s.isEmpty()
s.isBlank()
s.isNull()
s.isWhitespace()
Why is the check `if(s != null && !s.isEmpty())` considered a good practice?
It crashes if the string is null
It ensures the string is not null before checking if it is empty
It checks for whitespace in the string
It executes faster than other checks
What is the potential issue with the check `if(!s.isEmpty() && s != null)`?
It will not execute if the string is empty
It crashes if the string is null
It checks for null before checking if the string is empty
It is slower than other checks
Why should instance variables be private in a class?
To allow other classes to alter their values directly
To prevent other classes from directly altering their values
To make them accessible to all classes
To ensure they are always static
What is the recommended practice for instance variables that are not expected to change?
Make them public
Make them static
Make them final
Make them protected
What should you do if a variable belongs to a class?
Make it public
Make it static
Make it private
Make it final
What principle suggests giving the smallest possible access to your data?
Principle of Maximum Access
Principle of Least Privilege
Principle of Open Access
Principle of Data Sharing
Why is it important to include units in variable names?
To make the code look longer
To ensure clarity and prevent errors
To confuse other programmers
To reduce the number of variables
In a program that stores user information, which of the following is the best example of a descriptive and well-named variable?
x
userBirthDate
data1
tempVar
What is the entry point of a Java program where it begins execution?
The constructor method
The main method
The print method
The static method
In the Java main method, what must the arguments be declared as (from Jason's lecture)?
public
static
final
private
What does the keyword 'final' mean in Java?
A variable can be changed multiple times
A variable cannot change its value after it's set once
A variable is automatically initialized
A variable is only accessible within its class
In Java, how should variables be declared and initialized according to the conventions?
Declare and initialize in the same line
Declare in one line, initialize in a separate line
Initialize first, then declare
Declare and initialize in any order
In Java, what keyword is used to make one class inherit another?
inherit
extends
implements
super
Every Java class implicitly extends the Object class.
true
false
If a class does not define any constructor, what happens?
It cannot be instantiated
Compilation fails
It inherits constructors from the parent class
Java automatically provides a default constructor
The super() call must always be the first line in a constructor in all versions of Java.
true
false
Why is it a bad idea to make instance variables protected directly?
They become immutable
They cannot be inherited
They break encapsulation
They can be accessed outside the package
Using protected getters and setters is safer than making instance variables protected.
true
false
What is polymorphism?
Treating different child objects through the same parent type
Writing multiple classes with the same name
Converting data types
Overloading constructors
According to the Liskov Substitution Principle, a subclass should be usable anywhere its parent class is expected.
true
false
If Animal a = new Dog();, what is the static type of a?
Depends on runtime
Object
Dog
Animal
At runtime, overridden methods are chosen based on the object’s actual (dynamic) type.
true
false
Which of the following is a checked exception?
ArithmeticException
ArrayIndexOutOfBoundsException
IOException
NullPointerException
Checked exceptions must either be caught with try/catch or declared with throws.
true
false
Unchecked exceptions (RuntimeExceptions) must always be caught, or the program won’t compile.
true
false
What is the benefit of creating a custom exception class?
It prevents all crashes
It always improves performance
It replaces all standard exceptions
It makes error handling more readable and specific
Private methods and static methods in Java can be overridden.
true
false
Declaring a method as final prevents subclasses from overriding it.
true
false
Why are setter methods often declared final in parent classes?
To automatically generate getters
To ensure only the parent class controls how its fields are changed
To improve performance
To allow child classes to add more rules
A class declared as final cannot be extended.
true
false
By default, the equals() method in Java’s Object class compares:
Field values
Types only
Hash codes
Object memory addresses
If you override equals(), you don't have to override hashCode().
true
false
Which rule must hold between equals() and hashCode()?
Hash codes determine memory address
Equal objects must have different hash codes
Hash codes must always be unique
Equal objects must have equal hash codes
An abstract method has no body and must be implemented by a concrete subclass.
true
false
Which declaration is correct for an abstract method?
final abstract void move();
public static void move();
public void move();
public abstract void move();
Which of the following statements about abstract classes is correct?
They cannot have fields
They cannot contain constructors
They cannot contain any concrete methods
They cannot be instantiated
If a class has even one abstract method, the class itself must be declared abstract.
true
false
If a parent class has an abstract method, what must its child class do?
Implement it or declare itself abstract
Declare it final
Override it as static
Ignore it
The opposite of an abstract class is a concrete class.
true
false
Why would you use an abstract parent class with abstract methods?
To guarantee that all subclasses provide an implementation
To avoid using interfaces
To reduce compilation time
To make subclasses optional
Which keyword is used for a class to inherit from an interface?
interface
extends
inherits
implements
A Java class can implement multiple interfaces.
true
false
Which of the following can an interface contain?
Only abstract methods
Instance variables
Constructors
Abstract methods, constants, and default methods
Interfaces in Java can have constructors.
true
false
Which is a key difference between abstract classes and interfaces?
Abstract classes cannot contain constructors
Abstract classes cannot have methods
Interfaces allow multiple inheritance
Interfaces cannot contain constants
A class can extend one parent class but implement multiple interfaces.
true
false
By default, all variables in an interface are:
instance variables
public static final
private
protected
Constants declared in an interface must be initialized.
true
false
Interfaces can provide method bodies if the method is marked default.
true
false
Why were default methods introduced in interfaces?
To prevent multiple inheritance
To allow interfaces to have fields
To replace abstract classes
To allow backward compatibility when adding methods
The Comparable interface is used for:
Defining a natural ordering
Checking object equality
Comparing object references only
Defining multiple sort orders
Comparator allows us to define multiple custom orderings for objects.
true
false
If a class implements Comparable, which method must it override?
compare()
compareTo()
hashCode()
equals()
Which interface would you use if you need two different sorting logics for the same class?
Iterable
Cloneable
Comparable only
Comparator
You can NOT use both Comparable and Comparator together in the same program.
true
false
