The process of hiding the internal details of a class and showing only the things that are necessary is known as ________.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
In Java, encapsulation is the process of hiding the internal details of a class and exposing only the essential parts. It helps in maintaining the integrity of the class and prevents unauthorized access to its data. This concept is fundamental to object-oriented programming.
Which of the following event types is not a mouse event in JavaFX?
- KeyEvent
- MouseDragEvent
- MouseEvent
- TouchEvent
In JavaFX, KeyEvent is not a mouse event; it represents keyboard events. Mouse events, such as MouseEvent and MouseDragEvent, are related to mouse input. TouchEvent deals with touch input. Understanding the distinction between these event types is essential when working with JavaFX event handling.
Which exception type must be explicitly handled or declared to be thrown in a method?
- Checked exceptions
- Errors
- None of the above
- Unchecked exceptions (Runtime)
In Java, checked exceptions (which extend the Exception class but not RuntimeException) must be explicitly handled or declared to be thrown in a method. This requirement ensures that the code handles potentially problematic situations.
In which case(s) does Binary Search perform in O(1) time complexity?
- When the array is empty
- When the target element is at the first index of the array
- When the target element is at the middle of the array
- When the target element is not present in the array
Binary Search performs in O(1) time complexity when the target element is at the first index of the sorted array. In this case, it can directly access the element and return it. When the array is empty, it still performs in O(1) time complexity as there are no elements to search. In other cases, it performs in O(log n) time complexity, where 'n' is the number of elements in the array.
Which of the following is a valid synchronized method declaration?
- public synchronized void myMethod() {}
- public void myMethod() { synchronized() {} }
- public void synchronized myMethod() {}
- synchronized void myMethod() {}
The correct syntax for a synchronized method declaration in Java is public synchronized void myMethod() {}, where the synchronized keyword comes before the access modifier (public) and the return type (void). This ensures that only one thread can execute the myMethod at a time.
In Java, which of the following statements is true regarding the fall-through behavior in the switch case?
- In Java, when a case statement is matched, execution continues with the next case statement unless terminated by a break or other control flow statement.
- The fall-through behavior is controlled by the continue keyword.
- The fall-through behavior is controlled by the fallthrough keyword.
- The fall-through behavior is not supported in Java switch cases.
In Java, the switch statement supports fall-through behavior, meaning that when a case label is matched, the code will continue to execute the subsequent case labels unless interrupted by a break statement. This behavior allows multiple cases to be executed for a single condition match, making it important to use break to terminate the flow when needed.
Which of the following statements about constructor overloading in Java is correct?
- Constructor overloading allows a class to have multiple constructors with the same name but different parameters.
- Constructor overloading is not supported in Java.
- Constructor overloading requires that all constructors have the same number of parameters.
- Constructor overloading can only be used in abstract classes.
a) is correct. Constructor overloading in Java enables a class to have multiple constructors with the same name but different parameters. This allows for creating objects with various initializations. b) and d) are incorrect statements. c) is also incorrect because the number of parameters can vary among overloaded constructors.
The ________ interface in Java provides a way to traverse, filter, and extract data.
- Collection
- Iterable
- Iterator
- Stream
The Iterable interface in Java provides a way to traverse (iterate over) a collection of elements. It allows you to loop through the elements and perform operations on them. While it doesn't provide built-in filtering or extraction like Streams, it serves as a foundation for working with collections.
A method or a data member which is declared as ________ is accessible to all classes in the Java program.
- default (package-private)
- private
- protected
- public
In Java, when a method or data member is declared as "public," it is accessible to all classes, regardless of their location in the program. This is the highest level of visibility, and it allows unrestricted access from any part of the program.
The switch case in Java can be used with data types like _______, _______, and _______.
- byte, short, int
- char, long, byte
- double, string
- int, float, boolean
In Java, the switch case can be used with data types like char, long, and byte. While other data types like int, float, boolean, double, and String are commonly used in Java, they are not directly compatible with the switch case. This limitation is important to consider when using switch statements.