Java language basics quiz 1 contains 10 single and multiple choice questions. Java language basics quiz 1 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){
double d1 = Double.NaN;
double d2 = d1;
if(d1 == d2)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Correct answer.
Option 2 is the correct choice. Comparing NaN value with another NaN value returns false. Additionally, comparing NaN value with another non NaN value also returns false. Output of the above program will be “Not Equal”.
Incorrect answer.
Option 2 is the correct choice. Comparing NaN value with another NaN value returns false. Additionally, comparing NaN value with another non NaN value also returns false. Output of the above program will be “Not Equal”.
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){
int i = 10;
int j = 25;
System.out.println(i + ' ' + j);
}
}
Correct answer.
Option 3 is the correct choice. Java treats value in single quotes as characters, so they are promoted to int value while using arithmetic operations. ASCII value of space character is 32. So arithmetic expression will be like 10 + 32 + 25 = 67.
Incorrect answer.
Option 3 is the correct choice. Java treats value in single quotes as characters, so they are promoted to int value while using arithmetic operations. ASCII value of space character is 32. So arithmetic expression will be like 10 + 32 + 25 = 67.
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){
int i = 10;
int j = 12;
i+=++j;
System.out.println(i);
}
}
Correct answer.
Option 2 is the correct choice. Value of variable j is incremented before the assignment. Remember that ++j is a pre-increment, so value of j is incremented first and then assigned. i+=++j is short hand of i = i + ++j. Value of j becomes 13 after increment and then it is added to the value of i, thus value of i becomes 10+13 = 23.
Incorrect answer.
Option 2 is the correct choice. Value of variable j is incremented before the assignment. Remember that ++j is a pre-increment, so value of j is incremented first and then assigned. i+=++j is short hand of i = i + ++j. Value of j becomes 13 after increment and then it is added to the value of i, thus value of i becomes 10+13 = 23.
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 = 010;
System.out.println(i);
}
}
Correct answer.
Option 3 is the correct choice. The code will not give any compile time or run time error. If the number starts with 0, it is considered to be an octal number in Java. 010 is 8 in octal, so when you run the code, it will print 8 instead of 10.
Incorrect answer.
Option 3 is the correct choice. The code will not give any compile time or run time error. If the number starts with 0, it is considered to be an octal number in Java. 010 is 8 in octal, so when you run the code, it will print 8 instead of 10.
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){
byte b1 = 5, b2 = 2;
byte b = b1 % b2;
System.out.println(b);
}
}
Correct answer.
Option 4 is the correct choice. The Code will compile without any errors. The result of any arithmetic operation involving byte, char and short operands is automatically promoted to an int type in Java.
Since the int data type of the result is wider than the byte data type, it cannot be assigned back to the byte variable without explicit casting. Hence, the code will give compilation error “Type mismatch : cannot convert from int to byte”.
Incorrect answer.
Option 4 is the correct choice. The Code will compile without any errors. The result of any arithmetic operation involving byte, char and short operands is automatically promoted to an int type in Java.
Since the int data type of the result is wider than the byte data type, it cannot be assigned back to the byte variable without explicit casting. Hence, the code will give compilation error “Type mismatch : cannot convert from int to byte”.
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){
boolean b1 = true;
boolean b2 = true;
if(b1 == b2){
System.out.print("==");
}
if(b1.equals(b2)){
System.out.print("equals");
}
}
}
Correct answer.
Option 4 is the correct choice. This is a trick question which makes you think of == vs equals method.
The boolean (with small b) data type is a primitive type which does not have equals method and hence the code will not compile. So options 1, 2 and 3 are incorrect. Boolean (with capital B) is a wrapper class which has equals method.
Incorrect answer.
Option 4 is the correct choice. This is a trick question which makes you think of == vs equals method.
The boolean (with small b) data type is a primitive type which does not have equals method and hence the code will not compile. So options 1, 2 and 3 are incorrect. Boolean (with capital B) is a wrapper class which has equals method.
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){
int array[] = new int[1];
array[0] = 100;
System.out.println(array.length());
}
}
Correct answer.
Option 3 is the correct choice. length is a property of an array, not a method. Hence code will show compile time error. Option 3 is the correct choice.
Correct code to check the size of an array is array.length not array.length().
Incorrect answer.
Option 3 is the correct choice. length is a property of an array, not a method. Hence code will show compile time error. Option 3 is the correct choice.
Correct code to check the size of an array is array.length not array.length().
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test{
static String name = "Test";
public Test(){
name = "TestObject";
}
public static void main(String[] args){
System.out.println("Name is " + name);
}
}
Correct answer.
Option 1 is the correct choice. The name String variable is declared as static and initialized with string “Test”. The value of the name variable is changed in the constructor.
However, the class constructor is called only when an object is created. The code does not create any objects of the class Test, and hence the constructor is never called. So the value of the name variable remains unchanged.
Incorrect answer.
Option 1 is the correct choice. The name String variable is declared as static and initialized with string “Test”. The value of the name variable is changed in the constructor.
However, the class constructor is called only when an object is created. The code does not create any objects of the class Test, and hence the constructor is never called. So the value of the name variable remains unchanged.
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.2;
double d = 10.2;
if(f == d)
System.out.println("Same");
else
System.out.println("Not same");
}
}
Correct answer.
Option 3 is the correct choice. In Java, all the floating point literals like “10.2” are of type double by default. In order to assign this value to a float variable, the expression either needs an explicit cast or the literal needs to mention f or F at the end, for example, 10.2f.
The code will give compilation error at the line where the float variable is declared saying “Type mismatch: cannot convert from double to float”.
Incorrect answer.
Option 3 is the correct choice. In Java, all the floating point literals like “10.2” are of type double by default. In order to assign this value to a float variable, the expression either needs an explicit cast or the literal needs to mention f or F at the end, for example, 10.2f.
The code will give compilation error at the line where the float variable is declared saying “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{
public static void main(String args) {
int i = 5, j = 2;
System.out.println( i % j );
}
}
Correct answer.
Option 4 is the correct choice. The code will throw run time error. The code does not declare main method using correct syntax. Correct syntax of the main method is,
public static void main(String[] args)
The code has method named main but the argument is of type String instead of String array. Hence, the code will throw “Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)” when run.
Incorrect answer.
Option 4 is the correct choice. The code will throw run time error. The code does not declare main method using correct syntax. Correct syntax of the main method is,
public static void main(String[] args)
The code has method named main but the argument is of type String instead of String array. Hence, the code will throw “Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)” when run.