0

Java Programming Language Quiz - 5

Description: Java Programming Language Quiz - 5
Number of Questions: 10
Created by:
Tags: java
Attempted 0/10 Correct 0 Score 0

HashMap is thread-safe whereas Hashtable is not10, Which of the following assignment statements is invalid?

  1. double D = 45456.444;

  2. long L = 45784;

  3. int I = L;

  4. int J = (int) D;


Correct Option: C

Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?

  1. After object B is notified, or after two seconds

  2. After the lock on B is released, or after two seconds

  3. Two seconds after object B is notified

  4. Two seconds after lock B is released.


Correct Option: A

AI Explanation

To answer this question, let's break down each option:

Option A) After object B is notified, or after two seconds Option B) After the lock on B is released, or after two seconds Option C) Two seconds after object B is notified Option D) Two seconds after lock B is released

The correct answer is A) After object B is notified, or after two seconds.

Explanation: When the wait(2000) method is called on an object, it causes the current thread to release the lock on that object and enter a waiting state. The thread will remain in the waiting state until one of two conditions is met:

  1. The object is notified by another thread using the notify() or notifyAll() method.
  2. The specified time (2000 milliseconds in this case) has elapsed.

In this scenario, thread A will become a candidate to get another turn at the CPU either when object B is notified or after two seconds, whichever comes first. So option A is correct.

Option B is incorrect because the thread does not need to wait for the lock on object B to be released. The wait(2000) method already releases the lock.

Option C is incorrect because the thread does not wait for two seconds after object B is notified. It can become a candidate to get another turn at the CPU immediately after being notified.

Option D is incorrect because the thread does not wait for two seconds after releasing the lock on object B. It can become a candidate to get another turn at the CPU immediately after releasing the lock.

Therefore, the correct answer is A) After object B is notified, or after two seconds.

for(Object obj: expression) { } What interface must the ?expression? implement in order for you to use it with enhanced for loop construct?

  1. Iterator

  2. Map

  3. Enumeration

  4. Iterable


Correct Option: D

Which of the following are primitive types?

  1. Float

  2. None of the above

  3. byte

  4. String

  5. integer


Correct Option: C

What will happen when you attempt to compile and run the following code


public class Hope{
    public static void main(String argv[]){
        Hope h = new Hope();
    }
    protected Hope(){
        for(int i =0;
        i <10;
        i ++){
            System.out.println(i);
        }
    }
}
  1. Compilation and running with output 0 to 9

  2. Run time error: Constructors cannot be declared protected

  3. Compilation error: Constructors cannot be declared protected

  4. Compilation and running with output 0 to 10

  5. None of the above


Correct Option: A

What is output of the above program?

Class conditional {
 public static void main(String args[]) {
  int i = 20;
  int j = 55;
  int z = 0;
  z = i < j ? i : j; // ternary operator
  System.out.println("The value assigned is " + z);
 }
}
  1. 20

  2. 55

  3. None of the above


Correct Option: A

AI Explanation

To answer this question, let's go through the code step by step:

The given code snippet demonstrates the usage of the ternary operator to assign a value to the variable z based on a condition.

The ternary operator (condition) ? (expression1) : (expression2) works as follows:

  • If the condition is true, it evaluates to expression1.
  • If the condition is false, it evaluates to expression2.

In this case, the condition is i < j, where i is 20 and j is 55.

Since the condition i < j is true (20 is less than 55), the ternary operator will evaluate to i, which is 20.

Therefore, the value assigned to z is 20.

The output of the program will be: "The value assigned is 20"

Hence, the correct answer is option A) 20.

A class cannot be declared:

  1. Static

  2. Private

  3. Default

  4. None of the above


Correct Option: B

Following code will result in:

int a1 = 5; 
double a2 = (float)a1;
  1. Compilation error

  2. Runtime error

  3. No errors

  4. Execptions


Correct Option: C

AI Explanation

To answer this question, we need to understand the concept of type casting in Java.

In the given code, we have an integer variable a1 initialized with the value 5. Then, we have a double variable a2 which is assigned the value of a1 after type casting it to a float.

Now, let's go through each option to determine the correct answer:

Option A) Compilation error - This option is incorrect. The code will compile successfully because the type casting from int to float is allowed in Java.

Option B) Runtime error - This option is incorrect. The code will not result in a runtime error as the type casting is valid.

Option C) No errors - This option is correct. The code will execute without any compilation or runtime errors.

Option D) Exceptions - This option is incorrect. The code will not throw any exceptions.

Therefore, the correct answer is C) No errors. This option is correct because the code will execute without any compilation or runtime errors.

Following code will result in:

class A {
  int b = 1;
  public static void main(String [] args) {
       System.out.println("b is " + b);
 }
}
  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Output of b is 1


Correct Option: A

AI Explanation

To answer this question, you need to understand the concept of static and instance variables in Java.

In the given code, the variable b is an instance variable of the class A because it is declared within the class but outside any method or constructor.

The main method in Java is a static method, which means it belongs to the class itself and not to any specific instance of the class. Static methods can only access static variables directly. In this case, the main method is trying to access the instance variable b directly, which is not allowed.

Therefore, the given code will result in a compilation error.

The correct answer is A) Compilation error.

Following code will result in:

class A { 
   public static void main(String [] args) {
       B b = new A(); 
   }
} 
class B extends A {}
  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Other Execptions


Correct Option: A

AI Explanation

To answer this question, let's go through each option:

Option A) Compilation error - This option is correct. The code will result in a compilation error. The reason is that in the main method, an instance of class A is being assigned to a variable of type B. However, class A is not a subclass of class B, so this assignment is not allowed. This will result in a compilation error.

Option B) Runtime Error - This option is incorrect. A runtime error occurs when the code executes and encounters an error that prevents it from continuing. In this case, the code will not reach the runtime stage because it fails to compile.

Option C) Runtime Exception - This option is incorrect. A runtime exception occurs when an exception is thrown during the execution of the code. In this case, the code will not reach the runtime stage because it fails to compile.

Option D) Other Exceptions - This option is incorrect. Since the code fails to compile, it will not reach the stage where other exceptions can occur.

The correct answer is A) Compilation error. This option is correct because the code fails to compile due to the mismatch in the assignment of an instance of class A to a variable of type B.

- Hide questions