Can a subclass constructor directly access the private variables of its superclass?

  • No
  • Only if they are static
  • Only if they have the same name
  • Yes
No, a subclass constructor cannot directly access the private variables of its superclass. Private variables are not visible to subclasses, so they cannot be accessed or modified directly. Instead, you can use setter and getter methods or make the superclass variables protected or package-private (default) if you need subclass access.

How can you prematurely exit a loop in Java?

  • break statement
  • continue statement
  • goto statement (not recommended)
  • return statement
You can use the break statement to prematurely exit a loop in Java. When break is encountered within a loop, it immediately terminates the loop's execution, allowing you to exit the loop based on a certain condition or event. It is a commonly used control flow statement for loop termination.

You are working on a class that must not be instantiated and only serves to provide utility static methods. How would you utilize constructors to enforce this non-instantiability?

  • Define a constructor with a boolean parameter to control instantiation.
  • Define a private constructor to prevent instantiation and make the class final to prevent subclassing.
  • Make the class abstract with a default constructor for utility methods.
  • Utilize a public constructor with a warning message to discourage instantiation.
To enforce non-instantiability of a class meant for utility static methods, you should define a private constructor, preventing external instantiation. Additionally, making the class final ensures that it cannot be subclassed, further enforcing its intended usage as a utility class with only static methods.

Using the ________ method, you can run multiple SQL statements using a single Statement object, separated by semicolons.

  • execute()
  • executeBatch()
  • executeQuery()
  • executeUpdate()
The executeBatch() method in JDBC allows you to execute multiple SQL statements using a single Statement object, with statements separated by semicolons. This is particularly useful for batch processing. Other options are valid for executing single SQL statements.

Imagine you are tasked with creating a class structure for a game. How would you structure a class to represent a generic character, ensuring it is easy to create specific character subclasses later on?

  • A class with only instance variables for health, name, and level.
  • A class with public instance variables for health, name, and level.
  • An abstract class with methods for attack and defend.
  • An interface with abstract methods for attack and defend.
To create a generic character class for a game that can be easily extended for specific character types, you should use an abstract class. Abstract classes can contain instance variables for common properties and abstract methods for behaviors, allowing for easy subclassing while enforcing method implementations. An interface is not the best choice here as it lacks instance variables.

The ______ operator can be used to increment a variable by 1 in a postfix manner.

  • Assignment
  • Bitwise Shift
  • Decrement
  • Increment
The increment operator (++) can be used to increment a variable by 1 in a postfix manner. For example, if x is a variable, x++ will increment the value of x by 1 after its current value is used. This is often used in loops and other situations where you need to increment a variable.

If a class is declared as ________, it cannot be extended.

  • abstract
  • final
  • private
  • static
In Java, if a class is declared as final, it cannot be extended or subclassed. This is often used to prevent further modification or inheritance of a class, providing a level of immutability.

What happens when two methods are overloaded with array arguments of the type where one is an array of derived type and another is an array of its base type?

  • It depends on the order in which the methods are defined in the class.
  • The compiler throws an error as it cannot distinguish between the two overloaded methods.
  • The method with the array of the base type is called.
  • The method with the array of the derived type is called.
When two methods are overloaded with array arguments in Java, and one takes an array of a derived type while the other takes an array of its base type, the compiler throws an error. This is because the compiler cannot distinguish between the two methods based on the type of the array, as arrays in Java are covariant.

The showAndWait() method is used to display a ________ and wait for the user's response.

  • Alert
  • Dialog
  • Notification
  • Popup
In JavaFX, the Alert class is often used to display dialog boxes that require user interaction. The showAndWait() method is used to display an alert dialog and wait for the user's response before proceeding. The other options, Dialog, Popup, and Notification, are not the classes commonly associated with this method.

Which method is used to read a single character from a character input stream in Java?

  • read()
  • readChar()
  • readCharacter()
  • readLine()
The read() method in Java's character input stream classes is used to read a single character at a time. It returns an integer representing the Unicode code point of the character read. The other options are not valid methods for reading a single character from a character input stream.