Java language basics quiz 5 contains 10 single and multiple choice questions. Java language basics quiz 5 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
null is a valid keyword in Java
Correct answer.
False is the correct choice. null is technically not a keyword but a special literal which can be assigned to the object references as value. It cannot be assigned to primitive values though.
Incorrect answer.
False is the correct choice. null is technically not a keyword but a special literal which can be assigned to the object references as value. It cannot be assigned to primitive values though.
Question 2 of 10
2. Question
A constructor can be final
Correct answer.
False is the correct choice. A constructor cannot be overridden by the child class and thus cannot be final.
Incorrect answer.
False is the correct choice. A constructor cannot be overridden by the child class and thus cannot be final.
Question 3 of 10
3. Question
A transient variable can be static
Correct answer.
True is the correct choice. A transient variable can be static. On a side note, neither transient nor static variables are serialized by the default Java serializer.
Incorrect answer.
True is the correct choice. A transient variable can be static. On a side note, neither transient nor static variables are serialized by the default Java serializer.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 0, j = 1;
boolean b = getValue(i, j) ? true : false;
System.out.println(b);
}
public static boolean getValue(int i, int j){
return i > j;
}
}
Correct answer.
Option 2 is the correct choice. A ternary operator can have method call as the first expression as long as it returns a boolean value.
The code calls getValue method as the first expression of the ternary operator. Since the method returns false, expression after : will be evaluated which returns false and get assigned to variable b.
Incorrect answer.
Option 2 is the correct choice. A ternary operator can have method call as the first expression as long as it returns a boolean value.
The code calls getValue method as the first expression of the ternary operator. Since the method returns false, expression after : will be evaluated which returns false and get assigned to variable b.
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){
int i = 0;
System.out.println(i);
}
public static void changeMe(int i){
i++;
}
}
Correct answer.
Option 1 is the correct choice. In Java, the method parameters are passed by value. So even if the method changes the parameter value, the original variable in the caller method remains unchanged.
Incorrect answer.
Option 1 is the correct choice. In Java, the method parameters are passed by value. So even if the method changes the parameter value, the original variable in the caller method remains unchanged.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
double d = 10.0/0;
System.out.println(d);
}
}
Correct answer.
Option 4 is the correct choice. The code will not throw any exception and it will print “Infinity” when run.
Java float and double types can represent invalid values like positive infinity, negative infinity and NaN (not a number) value while int type cannot. When we run 10/0, it throws ArithmeticException as it is an integer operation but when we run 10.0/0, the operation is of floating point type which has infinity constant defined.
Incorrect answer.
Option 4 is the correct choice. The code will not throw any exception and it will print “Infinity” when run.
Java float and double types can represent invalid values like positive infinity, negative infinity and NaN (not a number) value while int type cannot. When we run 10/0, it throws ArithmeticException as it is an integer operation but when we run 10.0/0, the operation is of floating point type which has infinity constant defined.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
boolean b = "false";
if(b)
System.out.println("1");
else
System.out.println("2");
}
}
Correct answer.
Option 3 is the correct choice. Java boolean primitive type can have only one of the two possible literal values, true and false.
Wrapping the true or false literal in the double quotes makes it a string literal which is not a valid value for the boolean type. Code will give compilation error “Type mismatch: cannot convert from String to boolean”.
Incorrect answer.
Option 3 is the correct choice. Java boolean primitive type can have only one of the two possible literal values, true and false.
Wrapping the true or false literal in the double quotes makes it a string literal which is not a valid value for the boolean type. Code will give compilation error “Type mismatch: cannot convert from String to boolean”.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 10, j = 3;
int result = i/j;
System.out.println(result);
}
}
Correct answer.
Option 2 is the correct choice. Since both of the operands of the division operation are of type int, the result will be int. Since the int type cannot hold floating points, the decimal part of the result is discarded which gives us 3.
Incorrect answer.
Option 2 is the correct choice. Since both of the operands of the division operation are of type int, the result will be int. Since the int type cannot hold floating points, the decimal part of the result is discarded which gives us 3.
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[] intarr = new int[2];
System.out.println(intarr.length);
}
}
Correct answer.
Option 2 is the correct choice. The length property of an array returns the size of the array. Here, the array is declared to hold 2 elements of type int, so the size of the array is 2.
Remember, length is a property of an array, not a method. Correct way to get array size is array.length not array.length().
Incorrect answer.
Option 2 is the correct choice. The length property of an array returns the size of the array. Here, the array is declared to hold 2 elements of type int, so the size of the array is 2.
Remember, length is a property of an array, not a method. Correct way to get array size is array.length not array.length().
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){
int[] intarr = new int[2]{4, 5, 6};
for(int i = 0; i < intarr.length; i++)
System.out.print(intarr[i]);
}
}
Correct answer.
Option 3 is the correct choice. We cannot specify the size of an array along with array initialization values using curly braces.
Correct way to declare an array along with the initialization values would be int[] intarr = new int[]{4, 5, 6} without the size 2.
Incorrect answer.
Option 3 is the correct choice. We cannot specify the size of an array along with array initialization values using curly braces.
Correct way to declare an array along with the initialization values would be int[] intarr = new int[]{4, 5, 6} without the size 2.