Java Language Basics Quiz Online Test – 1

Time limit: 0

Quiz-summary

0 of 10 questions completed

Questions:

1

2

3

4

5

6

7

8

9

10

Information

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.

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”.

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.

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.

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.

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”.

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.

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.

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”.

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.