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.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 10 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
Language Basics0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
Which of the following are correct syntax for declaring an array?
Correct answer.
Option 1, 2, 5 and 6 are the correct choices.
Option 3 is incorrect because square brackets cannot come before the data type. Option 4 is incorrect because you cannot specify the size of an array along with the initialization values.
Incorrect answer.
Option 1, 2, 5 and 6 are the correct choices.
Option 3 is incorrect because square brackets cannot come before the data type. Option 4 is incorrect because you cannot specify the size of an array along with the initialization values.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
class Color{
String name;
public Color(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
public class Test{
public static void main(String[] args){
Color c1 = new Color("Red");
changeColor(c1);
System.out.print(c1.getName());
}
public static void changeColor(Color c){
c.setName("Blue");
System.out.print(c.getName());
}
}
Correct 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.
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.
Remember, only reference is being copied not the object itself.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
class Color{
String name;
public Color(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
public class Test{
public static void main(String[] args){
Color c1 = new Color("Red");
changeColor(c1);
System.out.print(c1.getName());
}
public static void changeColor(Color c){
c = new Color("Blue");
System.out.print(c.getName());
}
}
Correct 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.
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.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
final int i;
public static void main(String[] args){
System.out.println(i);
}
}
Correct 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”.
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”.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
import java.util.ArrayList;
import java.util.List;
public class Test{
public static void main(String[] args){
List listColors = new ArrayList<>();
listColors.add("Red");
listColors.add("Green");
listColors.add("Blue");
changeMe(listColors);
System.out.println(listColors);
}
private static void changeMe(final List listColors) {
listColors.add("Cyan");
listColors.remove("Blue");
}
}
Correct 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.
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
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.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
import java.util.ArrayList;
public class Test{
final ArrayList aList = new ArrayList<>();
public static void main(String[] args){
Test t = new Test();
t.aList.add("Hello");
}
}
Correct 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.
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.
ArrayList someOtherList = new ArrayList<>();
t.aList = someOtherList;
Question 8 of 10
8. Question
Will this code compile without any errors?
public class Test{
public static void main(String[] args){
int main = 0;
}
}
Correct answer.
Yes is the correct choice, main is not a keyword so it can be used as a variable name without any errors.
Incorrect answer.
Yes is the correct choice, main is not a keyword so it can be used as a variable name without any errors.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int[] arr = {0, 1, 2};
for(int i : arr)
System.out.print(i);
arr.length = 2;
for(int i : arr)
System.out.print(i);
}
}
Correct answer.
Option 3 is the correct choice. The length property of an array is a final field, it cannot be changed.
The code will give compilation error “The final field array.length cannot be assigned”.
Incorrect answer.
Option 3 is the correct choice. The length property of an array is a final field, it cannot be changed.
The code will give compilation error “The final field array.length cannot be assigned”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
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.