Java Declaration Initialization and Access Control Quiz 6
Time limit: 0
Quiz-summary
0 of 10 questions completed
Questions:
1
2
3
4
5
6
7
8
9
10
Information
Java declaration, initialization and access control quiz 5 contains 10 single questions. Declaration, initialization and access control quiz questions are designed in such a way that it will help you understand how to declare and initialize variables in Java as well as how to properly define access control for class, member variables and methods. At the end of the quiz, result will be displayed along with your score and quiz answers.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java Declaration, Initialization and Access Control 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
Declaration, Initialization and Access Control0%
Language Basics0%
OOPs0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
Will this code compile?
public class Test{
String className = "Test";
public static void main(String[] args) {
final Test t = new Test();
t.className = "Test.class";
}
}
Correct answer.
Yes is the correct choice. When an object reference is declared as final, it cannot be changed to point to other object later, however the content of the object it is pointing to can be changed.
Incorrect answer.
Yes is the correct choice. When an object reference is declared as final, it cannot be changed to point to other object later, however the content of the object it is pointing to can be changed.
Question 2 of 10
2. 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 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
Boolean b = new Boolean("TestClass");
System.out.println(b);
}
}
Correct answer.
Option 2 is the correct choice. If the string argument passed in Boolean(String s) constructor is not null and is equal to true ignoring case, then Boolean object representing true is created. For every other string value, Boolean object representing false will be created.
The code creates a Boolean object by passing string “TestClass” in the constructor, which is not null and does not equal to true so Boolean object representing false will be created. The code then prints the value of it which is false.
Incorrect answer.
Option 2 is the correct choice. If the string argument passed in Boolean(String s) constructor is not null and is equal to true ignoring case, then Boolean object representing true is created. For every other string value, Boolean object representing false will be created.
The code creates a Boolean object by passing string “TestClass” in the constructor, which is not null and does not equal to true so Boolean object representing false will be created. The code then prints the value of it which is false.
Question 4 of 10
4. 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 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
String[] strArray = new String[2];
System.out.println("Element: " + strArray[0]);
}
}
Correct answer.
Option 2 is the correct choice. Since String is an object in Java, it will be initialized with default value null regardless of whether it is defined at local or at class level.
Incorrect answer.
Option 2 is the correct choice. Since String is an object in Java, it will be initialized with default value null regardless of whether it is defined at local or at class level.
Question 6 of 10
6. 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 7 of 10
7. Question
A final method cannot be overloaded.
Correct answer.
False is the correct choice. A final method cannot be overridden in the subclass. However, it can be overloaded.
Incorrect answer.
False is the correct choice. A final method cannot be overridden in the subclass. However, it can be overloaded.
Question 8 of 10
8. Question
Will this code compile?
public class Test{
public static void main(String[] args){
int array[][] = new int[2][];
array[0] = new int[5];
array[1] = new int[10];
}
}
Correct answer.
Yes is the correct choice. It is mandatory to specify the first dimension of multidimensional array. However, other dimension can be left blank. It is also valid to have different lengths for different dimensions.
Incorrect answer.
Yes is the correct choice. It is mandatory to specify the first dimension of multidimensional array. However, other dimension can be left blank. It is also valid to have different lengths for different dimensions.
Question 9 of 10
9. Question
A final class can be abstract.
Correct answer.
False is the correct choice. A class can either be final or an abstract, not both.
Incorrect answer.
False is the correct choice. A class can either be final or an abstract, not both.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
float f1 = 10;
float f2 = 10.2;
System.out.println(f1 + "" + f2);
}
}
Correct answer.
Option 4 is the correct choice. 10 is considered as int literal and thus can be assigned to float variable. However, 10.2 is a double literal which cannot be assigned to float and will result in compilation error.
float f2 = 10.2f will be correct way to create a float variable.
Incorrect answer.
Option 4 is the correct choice. 10 is considered as int literal and thus can be assigned to float variable. However, 10.2 is a double literal which cannot be assigned to float and will result in compilation error.
float f2 = 10.2f will be correct way to create a float variable.