Which of the following keywords is used to create a new object in Java?

  • createObject
  • new
  • newInstance
  • newObject
In Java, the new keyword is used to create a new object. When you want to instantiate a class and create an object, you use the new keyword followed by the class constructor. For example, ClassName obj = new ClassName();. The other options are not used to create objects in Java.

Consider a scenario where you are developing a menu-driven application. The user is presented with different options, and based on the user's selection, a specific action is performed. Which control structure would be the most appropriate to handle this?

  • Do-while loop
  • For loop
  • Switch statement
  • While loop
In this scenario, a switch statement would be the most appropriate control structure. It allows you to efficiently handle multiple options and execute specific code blocks based on the user's selection. Each case represents a different option, making the code more readable and maintainable compared to a series of if-else statements. The for, while, and do-while loops are used for iterative operations, not menu-driven options.

Which class should be used when a string is going to be modified frequently?

  • StringArray
  • StringBuffer
  • StringBuilder
  • StringModifier
When a string is going to be modified frequently in Java, the StringBuilder class should be used. This class provides a mutable sequence of characters and is more efficient for string manipulation operations compared to String or StringBuffer. The other options, StringBuffer, StringArray, and StringModifier, are not standard Java classes for string manipulation.

How can you create a bi-directional binding between two properties in JavaFX?

  • By manually updating the properties.
  • Using the addBinding() method.
  • Using the bind() method.
  • Using the bindBidirectional() method.
To create a bi-directional binding between two properties in JavaFX, you can use the bindBidirectional() method provided by the javafx.beans.property package. This method establishes a two-way connection between the properties, ensuring that changes in one property are automatically reflected in the other and vice versa. This is a powerful feature for maintaining consistency between related properties.

In a lambda expression, a pair of parentheses is used to define ________.

  • Expressions
  • Parameters
  • Return Types
  • Variables
In a lambda expression, a pair of parentheses is used to define the parameters that the lambda expression takes. These parameters can be used within the lambda body to perform operations and computations.

Which method is used to add an element to an ArrayList?

  • add()
  • append()
  • insert()
  • push()
The add() method is used to add an element to an ArrayList in Java. It appends the specified element to the end of the list. Other methods like insert(), append(), and push() are not used for adding elements to ArrayLists.

How does Java handle method overloading with autoboxing and varargs?

  • Java allows method overloading, but it may lead to ambiguity.
  • Java automatically selects the method based on the argument types.
  • Java does not allow method overloading with autoboxing and varargs.
  • The compiler throws an error due to ambiguity.
In Java, method overloading with autoboxing and varargs is allowed. However, it should be used with caution, as it can lead to ambiguity in certain cases. Java will automatically select the most specific method based on the argument types provided. This behavior allows you to use overloaded methods with autoboxing and varargs, but you should be aware of potential ambiguities.

The method readResolve is used to replace the object read from the stream and should be declared with a return type of ______.

  • Object
  • Serializable
  • readObject
  • void
The readResolve method in Java is used to replace the object read from the stream with another object. It should be declared with a return type of Object to ensure that it can return the replacement object correctly. The other options are not appropriate return types for the readResolve method.

Which of the following statements correctly creates an object of a class named 'Dog'?

  • Dog dog = new Dog();
  • Dog obj = createObject();
  • Dog.create();
  • Dog.new();
To create an object of a class named 'Dog' in Java, you should use the class name followed by the constructor using the new keyword, like this: Dog dog = new Dog();. The other options are not the correct way to create an object of a class in Java.

To write a newline character when using FileWriter, you can use ________.

  • "n"
  • "n"
  • "r"
  • 'n'
To write a newline character when using FileWriter, you can use "n". This escape sequence represents a newline character in Java and is commonly used to create line breaks in text files.