Java language basics quiz 4 contains 10 single and multiple choice questions. Java language basics quiz 4 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
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
byte b1 = 10, b2 = 5;
byte b3 = b1 / b2;
System.out.println(b3);
}
}
Correct answer.
Option 3 is the correct choice. Arithmetic operations involving byte, short and char data types are automatically promoted to int type in Java.
Since the result is automatically promoted to int value, it cannot be assigned back to the byte variable (without explicit cast). The code will give compilation error “Type mismatch: cannot convert from int to byte”.
Incorrect answer.
Option 3 is the correct choice. Arithmetic operations involving byte, short and char data types are automatically promoted to int type in Java.
Since the result is automatically promoted to int value, it cannot be assigned back to the byte variable (without explicit cast). The code will give compilation error “Type mismatch: cannot convert from int to byte”.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args) {
byte b1 = 100, b2 = 28;
byte b3 = (byte) (b1+b2);
System.out.println(b3);
}
}
Correct answer.
Option 3 is the correct choice. Range of Java byte type is -128 to 127, so it cannot hold value which is greater than 127.
However, Java automatically promotes arithmetic operations with operands of byte, short and char types to int value. The code converts the result value back to the byte using explicit cast. Number 128 is above the range of byte (-128 to 127) so the value wraps around to 128 – 256 = -128.
Incorrect answer.
Option 3 is the correct choice. Range of Java byte type is -128 to 127, so it cannot hold value which is greater than 127.
However, Java automatically promotes arithmetic operations with operands of byte, short and char types to int value. The code converts the result value back to the byte using explicit cast. Number 128 is above the range of byte (-128 to 127) so the value wraps around to 128 – 256 = -128.
Question 3 of 10
3. Question
Which of the following lines will compile successfully if written at line 4?
public class Test{
public static void main(String[] args) {
//your code here
}
}
Correct answer.
Options 2, 5, 7 and 9 are the correct choices. The int variable cannot hold floating point numbers, so the option 1 is invalid.
From Java 7 onward, you can specify the underscore character (_) between digits any number of times to improve the readability of the code.
However, the underscore cannot appear at the start or at the end of the number, so the options 3 and 4 are not valid. If the number is a floating point number, then the underscore cannot appear immediately before or after the decimal point, hence the option 8 is not valid.
If a number is of type long or float, the underscore cannot appear immediately before f, F, l and L suffix, so the option 6 is not valid.
The underscore also cannot appear where the number sequence is expected. For example, in case of the hexadecimal number, underscore cannot appear immediately before or after “x”, so the options 10 and 11 are incorrect.
Incorrect answer.
Options 2, 5, 7 and 9 are the correct choices. The int variable cannot hold floating point numbers, so the option 1 is invalid.
From Java 7 onward, you can specify the underscore character (_) between digits any number of times to improve the readability of the code.
However, the underscore cannot appear at the start or at the end of the number, so the options 3 and 4 are not valid. If the number is a floating point number, then the underscore cannot appear immediately before or after the decimal point, hence the option 8 is not valid.
If a number is of type long or float, the underscore cannot appear immediately before f, F, l and L suffix, so the option 6 is not valid.
The underscore also cannot appear where the number sequence is expected. For example, in case of the hexadecimal number, underscore cannot appear immediately before or after “x”, so the options 10 and 11 are incorrect.
Question 4 of 10
4. Question
What are the correct syntaxes of the main method?
Correct answer.
Options 1, 4 and 5 are correct choices.
Option 2 is not valid because the parameter is not an array. Option 3 is not valid because main method needs to be public so that it can be called from outside. Option 6 is invalid because static keyword should come before return type of the method (i.e. void).
Incorrect answer.
Options 1, 4 and 5 are correct choices.
Option 2 is not valid because the parameter is not an array. Option 3 is not valid because main method needs to be public so that it can be called from outside. Option 6 is invalid because static keyword should come before return type of the method (i.e. void).
Question 5 of 10
5. Question
Which of the below given variable names are valid in Java?
Correct answer.
Options 1, 2, 3, 5, 6 and 7 are the correct choices. A variable name must begin with a letter, the dollar sign ($) or the underscore character (_). Subsequent characters may be letters, digits, $ or _. Any other special characters are not allowed in the variable name.
Incorrect answer.
Options 1, 2, 3, 5, 6 and 7 are the correct choices. A variable name must begin with a letter, the dollar sign ($) or the underscore character (_). Subsequent characters may be letters, digits, $ or _. Any other special characters are not allowed in the variable name.
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){
int try = 3;
for( int i = 0; i < try; i++){
System.out.print("Try" + i + " ");
}
}
}
Correct answer.
Option 4 is the correct choice. “try” is a keyword in Java and cannot be used as a variable name. The code will give compilation error “Syntax error on token “try”, invalid VariableDeclaratorId”.
Incorrect answer.
Option 4 is the correct choice. “try” is a keyword in Java and cannot be used as a variable name. The code will give compilation error “Syntax error on token “try”, invalid VariableDeclaratorId”.
Question 7 of 10
7. Question
Which of the following are Java keywords?
Correct answer.
Options 1 and 2 are the correct choices. Integer and Long are wrapper classes and not keywords in Java. You can use them as a variable name like “int Integer = 10;” without any compilation error.
Incorrect answer.
Options 1 and 2 are the correct choices. Integer and Long are wrapper classes and not keywords in Java. You can use them as a variable name like “int Integer = 10;” without any compilation error.
Question 8 of 10
8. Question
Which of the following are Java keywords?
Correct answer.
Options 1 and 4 are the correct choices. The goto and const are reserved keywords but not used in Java. null is a special literal which can be assigned as a value to any object reference (not to primitive) and false is a boolean literal.
Incorrect answer.
Options 1 and 4 are the correct choices. The goto and const are reserved keywords but not used in Java. null is a special literal which can be assigned as a value to any object reference (not to primitive) and false is a boolean literal.
Question 9 of 10
9. Question
Which of the following are NOT Java keywords?
Correct answer.
Options 3 and 6 are the correct choices. The class, strictfp, void, import and package are valid Java keywords. The correct keyword for extending a class is extends not extend. Likewise the correct keyword for implementing an interface is implements not implement.
Incorrect answer.
Options 3 and 6 are the correct choices. The class, strictfp, void, import and package are valid Java keywords. The correct keyword for extending a class is extends not extend. Likewise the correct keyword for implementing an interface is implements not implement.
Question 10 of 10
10. Question
What should be the type of the variable join in the following code at line 10?
public class Test{
public static void main(String[] args){
byte b = 20;
char c = 'a';
int i = 10;
float f = 20.2f;
join = b + c + i + f;
}
}
Correct answer.
Options 3 and 4 are the correct choices. Since the arithmetic expression involves float, the whole expression will be automatically widened to the float value.
A variable of float type can hold the float value. A double type can also hold the float value since it is wider than the float type.
Incorrect answer.
Options 3 and 4 are the correct choices. Since the arithmetic expression involves float, the whole expression will be automatically widened to the float value.
A variable of float type can hold the float value. A double type can also hold the float value since it is wider than the float type.