Java Language Basics Quiz Online Test – 6

Time limit: 0

Quiz-summary

0 of 10 questions completed

Questions:

1

2

3

4

5

6

7

8

9

10

Information

Java language basics quiz 6 contains 10 single and multiple choice questions. Java language basics quiz 6 questions are designed in such a way that it will help you understand the fundamental concepts of Java. At the end of the quiz, result will be displayed along with your score and Java language basics quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java language basics quiz online.

You have already completed the quiz before. Hence you can not start it again.

Option 1 is the correct choice. The code will output BlueBlue when compiled and run.

Method parameters are passed by value in Java. When it comes to an object as a method parameter, Java copies the object reference. It means that when you change the object properties using objects methods in the method which is called, the original object changes as well.

Remember, only reference is being copied not the object itself.

Incorrect answer.

Option 1 is the correct choice. The code will output BlueBlue when compiled and run.

Method parameters are passed by value in Java. When it comes to an object as a method parameter, Java copies the object reference. It means that when you change the object properties using objects methods in the method which is called, the original object changes as well.

Option 2 is the correct choice. The code will output BlueRed when compiled and run.

Method parameters are passed by value in Java. When it comes to an object as method parameter, Java copies the object reference. That means that the copy of the reference is still pointing to the original object so any change you made using object methods still reflects to the caller method when you access the object using original reference.

However, if you change the copied reference to point to new a object like we did here and change its properties, the original object remains unchanged.

Incorrect answer.

Option 2 is the correct choice. The code will output BlueRed when compiled and run.

Method parameters are passed by value in Java. When it comes to an object as method parameter, Java copies the object reference. That means that the copy of the reference is still pointing to the original object so any change you made using object methods still reflects to the caller method when you access the object using original reference.

However, if you change the copied reference to point to new a object like we did here and change its properties, the original object remains unchanged.

Option 3 is the correct choice. The final variables need to be initialized before they can be accessed.

The final instance variable can be initialized either at the time of the declaration or in the constructor of the class. The final static variable can be initialized either at the time of the declaration or in the static initializer block. The local final variables can be initialized either at the time of declaration or assigned the value only once after declaration before it can be accessed.

The code will give compilation error “The blank final field i may not have been initialized”.

Incorrect answer.

Option 3 is the correct choice. The final variables need to be initialized before they can be accessed.

The final instance variable can be initialized either at the time of the declaration or in the constructor of the class. The final static variable can be initialized either at the time of the declaration or in the static initializer block. The local final variables can be initialized either at the time of declaration or assigned the value only once after declaration before it can be accessed.

The code will give compilation error “The blank final field i may not have been initialized”.

Option 2 is the correct choice. In case of a final primitive value, its value cannot be changed. In case of a final object reference, it cannot be pointed to another object. However, the object methods can be called which may change the object state or properties.

In the above code we cannot write listColors = new ArrayList(); in changeMe method, because listColors parameter is final and we cannot assign a new object to it. However, calling add or remove method on the same object is valid. There will be no compilation error.

Java passes method parameters as value. In this case, the object reference is copied. The copied reference still points to the same original object so any operation you do, apart from assigning the reference to a new object, will be reflected in the original object as well.

Incorrect answer.

Option 2 is the correct choice. In case of a final primitive value, its value cannot be changed. In case of a final object reference, it cannot be pointed to another object. However, the object methods can be called which may change the object state or properties.

In the above code we cannot write listColors = new ArrayList(); in changeMe method, because listColors parameter is final and we cannot assign a new object to it. However, calling add or remove method on the same object is valid. There will be no compilation error.

Java passes method parameters as value. In this case, the object reference is copied. The copied reference still points to the same original object so any operation you do, apart from assigning the reference to a new object, will be reflected in the original object as well.

Question 6 of 10

6. Question

main is a keyword in Java

True

False

Correct answer.

False is the correct choice, main is not a keyword in Java. Since it is not a keyword, it is legal to declare a variable with name main. For example, int main = 0; will not give any compilation error.

Incorrect answer.

False is the correct choice, main is not a keyword in Java. Since it is not a keyword, it is legal to declare a variable with name main. For example, int main = 0; will not give any compilation error.

Yes is the correct choice. The code will compile without any errors. When any object reference is defined as final, it cannot be reassigned to another object. However, object’s methods can be called without any errors.

It would be illegal to write aList = new ArrayList<>(); in the main method, as this statement assigns a new object to the reference, which is not allowed for the final reference. The below given code also causes the compilation error as it also changes the object the final reference pointing to.

ArrayList someOtherList = new ArrayList<>(); t.aList = someOtherList;

Incorrect answer.

Yes is the correct choice. The code will compile without any errors. When any object reference is defined as final, it cannot be reassigned to another object. However, object’s methods can be called without any errors.

It would be illegal to write aList = new ArrayList<>(); in the main method, as this statement assigns a new object to the reference, which is not allowed for the final reference. The below given code also causes the compilation error as it also changes the object the final reference pointing to.

Option 4 is the correct choice. Since a1 and a2 are two different references pointing to two different array objects, == will return false.

The equals method of an array is inherited from the Object class which again just compares the references not the content of the objects they are pointing to. Again, since both of the references are pointing to different array objects, equals method will also return false.

To compare two arrays, use Arrays.equals method instead which gives proper result.

Incorrect answer.

Option 4 is the correct choice. Since a1 and a2 are two different references pointing to two different array objects, == will return false.

The equals method of an array is inherited from the Object class which again just compares the references not the content of the objects they are pointing to. Again, since both of the references are pointing to different array objects, equals method will also return false.

To compare two arrays, use Arrays.equals method instead which gives proper result.