Java Language Basics Quiz Online Test – 2

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 2 contains 10 single and multiple choice questions. Java language basics quiz 2 questions are designed in such a way that it will help you understand the basic concepts of Java language. 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 1 is the correct choice. The MyMathClass class has sum method overloaded for three different types of parameters, int double and String. However, the class does not have the method with float and char data types.

Java does widening conversions if the exact match is not found during the method invocation. So, the char value is widened automatically to the int and the float value is widened to the double value.

Incorrect answer.

Option 1 is the correct choice. The MyMathClass class has sum method overloaded for three different types of parameters, int double and String. However, the class does not have the method with float and char data types.

Java does widening conversions if the exact match is not found during the method invocation. So, the char value is widened automatically to the int and the float value is widened to the double value.

Options 2, 6, 7, 8, 9 and 10 are the correct choices. Narrower number types can be assigned to wider number types, for example, byte, short and char can be assigned to int, long, float and double.

Wider types cannot be assigned to narrower types, for example, long value cannot be assigned to the short or int type without explicit cast. String and other numerical types are not compatible at all so they cannot be assigned to one another.

Incorrect answer.

Options 2, 6, 7, 8, 9 and 10 are the correct choices. Narrower number types can be assigned to wider number types, for example, byte, short and char can be assigned to int, long, float and double.

Wider types cannot be assigned to narrower types, for example, long value cannot be assigned to the short or int type without explicit cast. String and other numerical types are not compatible at all so they cannot be assigned to one another.

Question 6 of 10

6. Question

Will this code compile without error?

public class Test{
public native int getArea(int w, int h);
}

Yes

No

Correct answer.

Yes is the correct choice. In Java, the native keyword is used to declare a method which is implemented in the language other than Java, mostly in C or C++, using Java Native Interface (JNI).

Some of the examples where native methods are used are 1) To implement the code utilizing system calls 2) To interface with the libraries written in other languages 3) To implement the functionality which is not available in Java

Incorrect answer.

Yes is the correct choice. In Java, the native keyword is used to declare a method which is implemented in the language other than Java, mostly in C or C++, using Java Native Interface (JNI).

Some of the examples where native methods are used are 1) To implement the code utilizing system calls 2) To interface with the libraries written in other languages 3) To implement the functionality which is not available in Java

Question 7 of 10

7. Question

Yes is the correct choice. All the methods defined in an interface are implicitly abstract. Specifying the abstract keyword in the method declaration is allowed in the interface but does not make any difference.

Hence, it is a valid method declaration and valid interface. The code will compile without any errors.

Incorrect answer.

Yes is the correct choice. All the methods defined in an interface are implicitly abstract. Specifying the abstract keyword in the method declaration is allowed in the interface but does not make any difference.

Hence, it is a valid method declaration and valid interface. The code will compile without any errors.

Option 3 is the correct choice. The int type is a primitive type and cannot have null value, only object reference can. The code will give compilation error “Type mismatch: cannot convert from null to int”.

Incorrect answer.

Option 3 is the correct choice. The int type is a primitive type and cannot have null value, only object reference can. The code will give compilation error “Type mismatch: cannot convert from null to int”.

Option 2 is the correct choice. The final and abstract keywords are opposite of each other.

Marking a class abstract means that the implementation is not complete, subclass has to provide the concrete implementation. While marking a class final means that the implementation is final and the behavior cannot be altered by extending it. Therefore, a class cannot be both, final and abstract at the same time.

The code will give compilation error “The class One can be either abstract or final, not both”.

Incorrect answer.

Option 2 is the correct choice. The final and abstract keywords are opposite of each other.

Marking a class abstract means that the implementation is not complete, subclass has to provide the concrete implementation. While marking a class final means that the implementation is final and the behavior cannot be altered by extending it. Therefore, a class cannot be both, final and abstract at the same time.

The code will give compilation error “The class One can be either abstract or final, not both”.

The code calls non-static compute method from the static main method without using object reference of the class Test, so it will give compilation error “Cannot make a static reference to the non-static method compute(int, int) from the type Test”.

The code calls non-static compute method from the static main method without using object reference of the class Test, so it will give compilation error “Cannot make a static reference to the non-static method compute(int, int) from the type Test”.