Java Declaration Initialization and Access Control Quiz 8
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 8 contains 10 single and multiple choice 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%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
Float f = new Float(10.0);
Double d = new Double(10.0);
if(f.equals(d))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Correct answer.
Option 2 is the correct choice. Equals method implemented by wrapper class checks if the object it is being compared to is of the same class. The code tries to compare Float object with Double object so the equals method returns false.
Remember, the objects being compared using equals method must of same type, otherwise equals method returns false.
Incorrect answer.
Option 2 is the correct choice. Equals method implemented by wrapper class checks if the object it is being compared to is of the same class. The code tries to compare Float object with Double object so the equals method returns false.
Remember, the objects being compared using equals method must of same type, otherwise equals method returns false.
Question 2 of 10
2. Question
What will happen when you compile and run the following code assuming file name is Test.java with below given commands?
javac Test.java
java Test
public class Test{
public void sayHi(){
System.out.print("Hi");
}
}
class AnotherTest{
public static void main(String[] args) {
System.out.print("Hello");
Test t = new Test();
t.sayHi();
}
}
Correct answer.
Option 4 is the correct choice. The code declares Test class as public and AnotherTest class with default modifier. There will not be any compilation error but when you try to run the code, the compiler expects main method inside the public class. It will give error “Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)” when you run the code.
Incorrect answer.
Option 4 is the correct choice. The code declares Test class as public and AnotherTest class with default modifier. There will not be any compilation error but when you try to run the code, the compiler expects main method inside the public class. It will give error “Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)” when you run the code.
Question 3 of 10
3. Question
Will this code compile without any errors?
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 4 of 10
4. Question
What will happen when you compile and run the following code placed inside file Test.java?
public class Test{
public static void main(String[] args) {
System.out.println("Hello");
new One().sayHi();
}
}
public class One{
public void sayHi(){
System.out.println("Hi");
}
}
Correct answer.
Option 3 is the correct choice. There cannot be more than one public class inside a Java file. Here code defines two classes with public modifier which will result in compilation error “The public type One must be defined in its own file”.
Incorrect answer.
Option 3 is the correct choice. There cannot be more than one public class inside a Java file. Here code defines two classes with public modifier which will result in compilation error “The public type One must be defined in its own file”.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test{
public void inc(int number){
static int count = number;
}
public static void main(String[] args) {
Test t1 = new Test();
t1.inc(1);
Test t2 = new Test();
t2.inc(2);
}
}
Correct answer.
Option 4 is the correct choice. Methods cannot have a static variable, only class can. The compiler will give error “Illegal modifier for parameter count; only final is permitted”.
Incorrect answer.
Option 4 is the correct choice. Methods cannot have a static variable, only class can. The compiler will give error “Illegal modifier for parameter count; only final is permitted”.
Question 6 of 10
6. Question
Can you declare a static method final?
Correct answer.
Yes is the correct choice. Yes it is legal to declare static method as final. The static method cannot be overridden by the subclass but subclass can hide the static method defined by the parent class by declaring method with the same signature. Making static method final in the parent class will prevent it.
Incorrect answer.
Yes is the correct choice. Yes it is legal to declare static method as final. The static method cannot be overridden by the subclass but subclass can hide the static method defined by the parent class by declaring method with the same signature. Making static method final in the parent class will prevent it.
Question 7 of 10
7. Question
Will this code compile successfully?
public class Test{
public static void main(String[] args) {
int[] intArray[] = new int[5][];
}
}
Correct answer.
Yes is the correct choice. It is legal way to declare multi-dimensional array. Plus, defining the size of first dimension is mandatory, rest can be left blank.
Incorrect answer.
Yes is the correct choice. It is legal way to declare multi-dimensional array. Plus, defining the size of first dimension is mandatory, rest can be left blank.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public Test(int num){
num = num * 5;
System.out.print(num);
}
public static void main(String[] args) {
int num = 1;
Test t = new Test(num);
System.out.print(num);
}
}
Correct answer.
Option 1 is the correct choice. The code will print 51. Java is “Pass by value” language, that means it copies the value during method calls. The code passes the variable num in constructor. The num variable in the constructor is local only to the constructor. Any modification done to it will not reflect back in the main method.
Incorrect answer.
Option 1 is the correct choice. The code will print 51. Java is “Pass by value” language, that means it copies the value during method calls. The code passes the variable num in constructor. The num variable in the constructor is local only to the constructor. Any modification done to it will not reflect back in the main method.
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){
float f = 10.0/2f;
System.out.println(f);
}
}
Correct answer.
Option 3 is the correct choice. The 10.0 is considered as double literal and hence 10.2/2f expression will be of double type. Double cannot be directly assigned to float type without explicit cast. The program will give compilation error “Type mismatch: cannot convert from double to float”.
Incorrect answer.
Option 3 is the correct choice. The 10.0 is considered as double literal and hence 10.2/2f expression will be of double type. Double cannot be directly assigned to float type without explicit cast. The program will give compilation error “Type mismatch: cannot convert from double to float”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
static int i;
public static void main(String[] args) {
Test t1 = new Test();
t1.i = 1;
Test t2 = new Test();
t2.i = 2;
System.out.println(t1.i + " " + t2.i);
}
}
Correct answer.
Option 1 is the correct choice. The static variable is a class level variable, that means it is shared among all the objects of the class. So t1.i and t2.i refers to the same int variable i. Since the latest assignment to the i variable is 2, 2 2 will be printed when you run the code.
Java compiler does not give any errors if you access the static variables using objects, however it is a good practice to always access the static variable using class name instead of object name.
Incorrect answer.
Option 1 is the correct choice. The static variable is a class level variable, that means it is shared among all the objects of the class. So t1.i and t2.i refers to the same int variable i. Since the latest assignment to the i variable is 2, 2 2 will be printed when you run the code.
Java compiler does not give any errors if you access the static variables using objects, however it is a good practice to always access the static variable using class name instead of object name.