Java Intermediate Assessment Quiz
Java Intermediate Assessment Quiz
The expression `true && false || true` in Java evaluates to `true`. First, the `&&` operator between `true` and `false` results in `false`. Then, the `||` operator examines `false || true`, which evaluates to `true` since the `||` (OR) operation is true if at least one operand is true. Hence, the overall expression evaluates to `true` .
'String' is not considered a primitive data type in Java because it is a reference type. Primitive data types, such as byte, short, int, and float, store their values directly in memory, whereas references to instances of classes like String are stored as references or pointers to the actual memory location of the data .
In Java, the `for` and `while` loops check their condition before executing the loop body. If the condition is false at the outset, the loop body is never executed. In contrast, a `do-while` loop executes its body at least once because the loop condition is evaluated after the loop body has executed. This ensures that the loop body is run at least once regardless of the condition .
The `start()` method in Java is crucial for starting a new thread of execution. When `start()` is called on a Thread object, it invokes the `run()` method in a separate call stack, effectively creating a new concurrent thread. If `run()` is called directly instead of `start()`, the code within the `run()` method executes in the current thread rather than a new one, thus not achieving parallel execution .
The `final` keyword in Java, when used with a method, prevents that method from being overridden in any subclass. This is important for ensuring the consistency and integrity of the method's behavior across all instances of the class hierarchy. By marking a method as `final`, developers can be assured that the method's implementation remains unchanged and behaves as initially intended .
Private methods in Java, such as `show()` in class A, are not accessible to subclasses, meaning they cannot be overridden. When class B extends A and defines its own `show()` method, it is not considered an override of A's method because A's `show()` is not visible to B. This results in method hiding, not method overriding, since B's `show()` is a completely separate method .
The output of `10 + 20 + "30"` in Java is "3030". This is because the expression is evaluated from left to right. First, `10 + 20` is calculated as `30` since both are integers. The result of this addition is then concatenated with the String "30", resulting in the final output of "3030" .
The outcome of the code `System.out.println(5/2);` in Java is `2`. This happens because both operands of the division are integers, resulting in integer division (truncating the fractional part) which discards the decimal portion and rounds the result towards zero, giving the final output as `2` .
In Java, the default (or package-private) access modifier allows classes, methods, and fields to be accessible only within the same package. There is no visibility outside the package unless the class or member is expressly marked public or protected. This access level is useful for encapsulating code and reducing exposure to unintended interactions across different packages .
Making the main method private in a Java program leads to a runtime error when attempting to execute the program. The `main` method serves as the entry point for JVM execution, and it must be declared public to be accessible by entities outside the class, such as the JVM itself. If it's private, the JVM cannot access it, preventing program execution .