Java Quiz (True/Flase)

TRUE / FALSE …


1. Declarations must appear at the start of the body of a Java method.

False
They can appear anywhere within the body of the method.

2. All bitwise operations are carried out with the same level of precedence in Java.

Answer
False
All operations in Java, including the bitwise operations, are carried out with a definite precedence.

3. The expression (y >= z && a == b) is evaluated by first evaluating the expression y >= z, and then evaluating a == b.

Answer
False
If y >= z is false, then there is no need to evaluate the second expression.

4. The “switch” selection structure must end with the default case.

Answer
False
The default case in a switch structure is optional and only used when none of the other cases match.

5. For the expression (y >= z && a == b) to be true, at least one of (y >= z) and (a == b) must be true.

Answer
False
For the result of a “&&” operation to be true, both operands must be true.

6 A break statement must always be present in the default case of a “switch” selection structure.

Answer

7. Variables declared inside a for loop are limited in scope to the loop.

Answer
False
Any variable declared within a block statement such as a for or if cannot be referenced outside the block.

8. A method in a class declared as static can only access static class members.

Answer
True
Static methods may, however, receive objects as parameters.

9 A method in a class declared as static may be invoked simply by using the name of the method alone.

Answer
False
This is true when the call is made from within the same class. However, when a static method is invoked from another class, then both the classname as well as the method name must be used to make the call.

10. Each method in a class must have a unique name.

Answer
False
Two (or more) methods can have the same name but must have different parameters. For example, “int getHeight(int x)” and “int getHeight(String s)” can be the names of two methods in a class. This is called overloading of methods.

11. Java does not allow a method with the same signature in a subclass, as a method in the super class.

Answer
False
When a method in a subclass had the same signature as a method in a parent class, then it is said to “override” that method.

12.The == operator can be used to compare two String objects. The result is always true if the two strings are identical.

Answer
False
String objects must be compared using the “equals” method of one of the objects. Food for thought: will the == operator ever return true when two string objects are compared using it?

13. A method declaration must always contain the access level.

Answer
False
When an access level is not declared, the method gets a package access level by default.

14.Objects of a subclass can be assigned to a super class reference.

Answer
True
Objects of a super class may not be assigned to a sub class reference. Food for thought: why is it so?

15. The return value from a method must always match the declared return type.

Answer
True
When a method returns an object, it must be an instance of either the exact class declared as the return type, or its subclass.

16. A method that is overridden in the subclass must retain the same return type and parameter list.

Answer
True
The overriding method may, however, have a different throws clause as long as it only reduces the number of exceptions being thrown in the overridden method.

17. The access level of an overridden method cannot be changed in a subclass.

Answer
False
The overriding method can increase the access to the overridden method. For instance, a protected method may be made public, but not private.

18. The modulus operator (%) in Java can be used only with variables of integer type.

Answer
False
The modulus operator (%) may be used with floating-point as well as integer types. It returns the remainder of a division operation, e.g., 10 % 6 will return 4.

19. When an instance of a class, or object, is specified as a parameter to a method, a reference to the said object is passed to the method.

Answer
True
Java only passes the reference to an object to a method.

20. The operations y >> 3 and y >>> 3 produce the same result when y > 0.

Answer
True
The shift operation “y1 >>> y2” is identical to “y1 >> y2” for all positive values of y1. It shifts the bits in y1 to the right by y2 positions.

21. consider the statement “x = (a > b) ? a : b”; then the value of x is 27, if a = 18 and b = 27.

Answer

22. Whenever the “&&” operator is used, such as in:
exp1 && exp2
where exp1 and exp2 are boolean expressions, both the boolean expressions are not always evaluated.

Answer
True
If the first expression is false, the result of the “&&” operation will always be false regardless of the second expression. The “&” operator on the other hand forces the evaluation of the second expression even if the first expression is false.

23. A static method can refer to any instance variable of the class.

Answer
False
It is illegal in Java to refer to instance variables from a static method.

24. All interface methods must be declared as public when implemented in a class.

Answer
True
Interfaces are abstraction of functionality that can be implemented in more than one way when required. The abstraction must be made available as public for the use by other parts of the program.

25. Methods can be overloaded with a difference only in the type of the return variable.

Answer
False
Method overloading (having two methods with the same name within a class) requires that parameters to the methods must be different. Food for thought: can you explain why?

26. An array in the Java programming language has the ability to store many different types of values.

Answer
False
All elements of an erray must be of the same type. However, it is possible to declare an array of objects, which may of instances of different classes.

27. An individual array element from an array of type int, when passed to a method is passed by value.

Answer
True
Individual elements of an array of this type are passed by value as a parameter to a method. Any changes to them in the method will not change the value of the array element.

28. Objects of a super class can always be assigned to a subclass reference.

Answer
False
Objects of a subclass may be assigned to a super class reference. Food for thought: is there a loss in functionality when this is done?

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.