1.
Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
Correct Answer
D. All statements are correct.
Explanation
Explanation: Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not necessary, because a long can store a byte.
2.
Which of these values can a boolean variable contain?
Correct Answer
A. True and False
Explanation
Explanation: Boolean variable can contain only one of two possible values, true and false.
3.
Which of these can be returned by the operator & ?
Correct Answer
D. Integer or Boolean
Explanation
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it returns a boolean).
4.
What is the value stored in x in following lines of code?int x, y, z;x = 0;y = 1;x = y = z = 8;
Correct Answer
D. 8
Explanation
Explanation: none
5.
Which of these are selection statements in Java?
Correct Answer
A. If()
Explanation
Explanation: continue and break are jump statements, and for is an looping statement.
6.
Which of the following is a method having same name as that of its class?
Correct Answer
B. Constructor
Explanation
A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides
7.
On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit?
Correct Answer
B. 31
Explanation
Explanation: The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from the right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.
8.
Which of these cannot be declared static?
Correct Answer
B. Object
Explanation
Explanation: static statements are run as soon as class containing then is loaded, prior to any object declaration.
9.
A class member declared protected becomes member of subclass of which type?
Correct Answer
B. Private member
Explanation
Explanation: A class member declared protected becomes private member of subclass.
10.
Which of this method of an Object class can clone an object?
Correct Answer
C. Object clone()
Explanation
The correct answer is "Object clone()". This method is a part of the Object class in Java and is used to create a copy or clone of an object. It returns a new object that is a copy of the original object.
11.
Which of this method of class String is used to obtain the length of String object?
Correct Answer
D. Length()
Explanation
Explanation: Method length() of string class is used to get the length of the object which invoked method length().
12.
Which of these method of class StringBuffer is used to concatenate the string representation to the end of invoking string?
Correct Answer
B. Append()
Explanation
The method append() of class StringBuffer is used to concatenate the string representation to the end of the invoking string. This method appends the specified string to the existing string and returns a reference to the modified StringBuffer object. It is commonly used to build longer strings by appending smaller strings together.
13.
Which of these keywords can be used to prevent Method overriding?
Correct Answer
D. Final
Explanation
Explanation: To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.
14.
Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
Correct Answer
A. GetBytes()
Explanation
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions provided by the platform.
15.
Which of these is a mechanism for naming and visibility control of a class and its content?
Correct Answer
B. Packages
Explanation
Explanation: Packages are both naming and visibility control mechanism. We can define a class inside a package which is not accessible by code outside the package.
16.
Which of these is an interface for control over serialization and deserialization?
Correct Answer
B. Externalization
Explanation
Externalization is an interface for control over serialization and deserialization. It allows developers to have more control over the serialization process by implementing the Externalizable interface and providing their own custom serialization logic. This interface provides two methods, writeExternal() and readExternal(), which allow objects to write and read their own state in a customized manner. This is different from the Serializable interface, which provides a default serialization mechanism. FileFilter and ObjectInput are not interfaces for control over serialization and deserialization.
17.
Which of these methods is used to compare a specific region inside a string with another the specific region in another string?
Correct Answer
D. RegionMatches()
Explanation
The method regionMatches() is used to compare a specific region inside a string with another specific region in another string.
18.
Which of these keywords is not a part of exception handling?
Correct Answer
C. Thrown
Explanation
Explanation: Exceptional handling is managed via 5 keywords – try, catch, throws, throw and finally.
19.
Which of these is a process of extracting/removing the state of an object from a stream?
Correct Answer
D. Deserialization
Explanation
Explanation: Deserialization is a process by which the data written in the stream can be extracted out from the stream.
20.
Which of these is a super class of wrappers Double and Float?
Correct Answer
D. Number
Explanation
Explanation: Number is an abstract/super class containing subclasses Double, Float, Byte, Short, Integer and Long.