0

Basic Java Programming Quiz 2

Description: Basic Java Programming Quiz 2
Number of Questions: 13
Created by:
Tags: java
Attempted 0/13 Correct 0 Score 0

Methods that are marked protected can be called in any subclass of that class.

  1. True

  2. False


Correct Option: A

An abstract class can have non-abstract methods.

  1. True

  2. False


Correct Option: A
Explanation:

The answer is: A

Explanation:

An abstract class can contain both abstract and non-abstract methods. Abstract methods are those that do not have an implementation, while non-abstract methods have a complete implementation. However, any class that contains at least one abstract method must be declared as abstract, which means that it cannot be instantiated directly.

Java keywords are written in lowercase as well as uppercase.

  1. True

  2. False


Correct Option: B

What is an instanceof

  1. A methods in object

  2. An operator and keyword


Correct Option: B
Explanation:

To solve this question, the user needs to have knowledge about programming concepts and specifically about the instanceof operator.

Now, let's go through each option and explain why it is right or wrong:

A. A methods in object: This option is incorrect. instanceof is not a method in an object. It is an operator that is used to check if an object belongs to a particular class or its subclasses.

B. An operator and keyword: This option is correct. instanceof is both an operator and a keyword in programming languages like Java. It is used to determine if an object is an instance of a particular class or its subclasses. The instanceof operator returns true if the object is an instance of the specified class or a subclass, and false otherwise.

Therefore, the correct answer is:

B. An operator and keyword

Primitive datatypes are allocated on a stack.

  1. True

  2. False


Correct Option: A

AI Explanation

To answer this question, we need to understand the concept of primitive data types and the stack.

Primitive data types, such as integers, floats, booleans, and characters, are basic data types provided by programming languages. They store simple values and have a fixed size.

The stack is a region of memory used for local variables and function calls. It follows a LIFO (Last-In, First-Out) principle, where the last item pushed onto the stack is the first one to be popped off.

Now, let's go through the options:

Option A) True - This option is correct. Primitive data types are typically allocated on the stack. When a primitive variable is declared and assigned a value, it is stored on the stack. The memory allocated for the variable is freed when it goes out of scope or when the function it is defined in returns.

Option B) False - This option is incorrect. Primitive data types are not allocated on the heap, but rather on the stack.

The correct answer is Option A) True. Primitive data types are allocated on the stack.

Can you compare a boolean to an integer?

  1. Yes

  2. No


Correct Option: B

If class A implements an interface does it need to implement all methods of that interface?

  1. Yes, always.

  2. No, not when A is abstract


Correct Option: B

Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?

  1. Compiler error

  2. Runtime Exception

  3. True

  4. False


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concept of object comparison in Java and how it differs from primitive type comparison.

In Java, objects are compared using the == operator. When comparing two objects using ==, Java checks if the two objects refer to the same memory location. If they do, the == operator returns true; otherwise, it returns false.

Now let's go through each option and explain why it is right or wrong:

A. Compiler error: This option is incorrect. There is no compilation error in the given code. The code will compile successfully.

B. Runtime Exception: This option is incorrect. There is no runtime exception in the given code. The code will run without throwing any exceptions.

C. True: This option is incorrect. Although the values of a and b are both 2, the variables a and b are objects of the Integer class. When comparing objects using ==, Java checks if they refer to the same memory location, not their values. In this case, a and b are different objects, even though their values are the same. Therefore, a == b will return false.

D. False: This option is correct. As explained earlier, a and b are different objects, even though their values are the same. Therefore, a == b will return false.

The answer is: D. False

The methods wait(), notify() and notifyAll() in Object need to be called from synchronized pieces of code.

  1. True

  2. False


Correct Option: A

Inner classes can be defined within methods.

  1. True

  2. False


Correct Option: A

Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.

  1. True

  2. False


Correct Option: A
Explanation:

The statement is True.

The "synchronized" keyword in Java is used to achieve thread synchronization. When a method or a block of code is declared as synchronized, it means that only one thread can access that method or block at a time.

When a thread encounters a synchronized method or block, it first checks if the lock associated with the object is available. If the lock is available, the thread grabs the lock and proceeds with executing the code inside the synchronized method or block. After the execution is complete, the lock is released, allowing other threads to access the synchronized code.

So, the "synchronized" keyword ensures that a thread grabs an object lock before continuing execution, making the statement True.

The answer is: A. True

The default statement of a switch is always executed.

  1. True

  2. False


Correct Option: B
Explanation:

To evaluate this statement, we need to understand the behavior of the default statement in a switch statement.

The default statement in a switch statement is optional and serves as a catch-all case. It is executed if none of the other cases match the value being switched.

Now let's go through each option:

A. True: This option is incorrect. The default statement is only executed if none of the other cases match the value being switched. If any of the other cases match, the default statement is skipped.

B. False: This option is correct. The default statement is not always executed. It is only executed if none of the other cases match the value being switched.

Therefore, the correct answer is B. False.

Let me know if you have any further questions!

How can you prevent a member variable from becoming serialized?

  1. By marking it private

  2. By marking it volatile

  3. By marking it transient

  4. You can not.


Correct Option: C
- Hide questions