1.
What is the size of a Char?
Correct Answer
D. 16 bits
Explanation
The size of a Char is 16 bits. This means that a Char data type can store values ranging from -32,768 to 32,767.
2.
A class cannot be declared
Correct Answer
B. Private
Explanation
A class cannot be declared as private because the access modifier "private" restricts the visibility of members to only within the same class. However, a class needs to be accessible outside of its own class in order to be used by other classes or objects. Therefore, a class can be declared as static or default, but not as private.
3.
Following code will result in: int a = 3.5;
Correct Answer
A. Compilation error
Explanation
The code will result in a compilation error because the variable "a" is declared as an integer, but it is being assigned a value of 3.5, which is a floating-point number. In Java, you cannot assign a floating-point value to an integer variable without explicitly casting it.
4.
Following code will result in: int a1 = 5; double a2 = (float)a1;
Correct Answer
C. No errors
Explanation
The given code will not result in any errors. It declares an integer variable "a1" with a value of 5 and a double variable "a2" which is assigned the value of "a1" after casting it to a float. Since the casting is allowed and there are no other issues with the code, it will compile and run without any errors.
5.
Following code will result in: int a = 9/0;
Correct Answer
C. Runtime Exception
Explanation
The code will result in a Runtime Exception because it is attempting to divide 9 by 0, which is not possible and will cause an ArithmeticException to be thrown at runtime.
6.
Following code will result in: float a = 9/0;
Correct Answer
D. No Error: a is NaN
Explanation
The code will not result in a compilation error or a runtime exception. Instead, it will assign the value NaN (Not a Number) to the variable 'a'. This is because dividing any number by zero in floating-point arithmetic results in NaN.
7.
A class can be transient
Correct Answer
B. False
Explanation
A class cannot be transient. The "transient" keyword in programming languages is used to indicate that a variable should not be serialized, meaning it will not be included when the object is converted into a stream of bytes. However, the "transient" keyword cannot be applied to a class itself. It can only be used with variables within a class. Therefore, the correct answer is False.
8.
Following code will result in: class A { int b = 1; public static void main(String [] args) { System.out.println("b is " + b); }}
Correct Answer
A. Compilation error
Explanation
The code will result in a compilation error because the variable "b" is an instance variable and cannot be accessed directly from a static method (main method). To access the instance variable, an object of class A needs to be created.
9.
Following code will result in: class A { public static void main(String [] args) {B b = new A(); }} class B extends A {}
Correct Answer
A. Compile error
Explanation
The code will result in a compile error because it is trying to create an instance of class A using the constructor of class B. Since class B extends class A, it is possible to create an instance of class B using the constructor of class A, but not the other way around. Therefore, the code is not valid and will not compile.
10.
Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}
Correct Answer
C. No errors
Explanation
The given code will not result in any errors because class B is a subclass of class A, so it is valid to assign an instance of B to a variable of type A. Therefore, the code will compile successfully and run without any issues.
11.
Methods that are marked protected can be called in any subclass of that class.
Correct Answer
A. True
Explanation
Protected methods in a class can be accessed by any subclass of that class. This means that even if the subclass is in a different package or module, it can still call the protected method. This is because protected access allows for inheritance and provides a way for subclasses to access and modify the behavior of the superclass. Therefore, the given answer, which states that protected methods can be called in any subclass of the class, is true.
12.
An abstract class can have non-abstract methods.
Correct Answer
A. True
Explanation
An abstract class can have non-abstract methods because an abstract class is a class that cannot be instantiated and is meant to be extended by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class can have a defined implementation, allowing the abstract class to provide common functionality to its subclasses. Subclasses that extend the abstract class can inherit and use these non-abstract methods without needing to redefine them. Therefore, it is true that an abstract class can have non-abstract methods.
13.
Java keywords are written in lowercase as well as uppercase
Correct Answer
B. False
Explanation
Java keywords are written in lowercase only. This is because Java is case-sensitive, meaning that uppercase and lowercase letters are treated as different characters. Therefore, using uppercase letters in keywords would result in a compilation error.
14.
What is an instanceof
Correct Answer
B. An operator and keyword
Explanation
The correct answer is "An operator and keyword". The instanceof operator is used to check if an object belongs to a certain class or implements a certain interface. It returns true if the object is an instance of the specified class or interface, and false otherwise. It is a keyword in Java that is used in conjunction with the operator to perform this check.
15.
Primitive datatypes are allocated on a stack.
Correct Answer
A. True
Explanation
Primitive datatypes are allocated on a stack because they are small and have a fixed size. The stack is a region of memory that is used for local variables and function calls. When a primitive datatype is declared, memory is allocated on the stack to store its value. This memory is automatically freed when the variable goes out of scope. This is in contrast to objects, which are allocated on the heap and require manual memory management.
16.
Can you compare a boolean to an integer?
Correct Answer
B. No
Explanation
A boolean represents a binary value, either true or false, while an integer represents a numerical value. These two types are not directly comparable as they have different representations and purposes. In most programming languages, attempting to compare a boolean to an integer would result in a type error. Therefore, the correct answer is "No".
17.
Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?
Correct Answer
D. False
Explanation
When comparing two objects using the == operator, it checks if the two objects are referring to the same memory location. In this case, since both objects a and b are created using the new keyword, they are stored in different memory locations even though they have the same value. Therefore, the condition (a==b) evaluates to false.
18.
Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.
Correct Answer
A. True
Explanation
The explanation for the given correct answer is that the "synchronized" keyword in Java is used to ensure that only one thread can access a block of code or an object at a time. It grabs an object lock before executing the synchronized code, preventing other threads from accessing it simultaneously. This helps in preventing race conditions and maintaining data integrity in multi-threaded environments. Therefore, the statement "Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution" is true.
19.
The default statement of a switch is always executed
Correct Answer
C. By marking it transient
20.
Which one does not extend java.lang.Number
Correct Answer(s)
B. Boolean
C. Character
Explanation
The correct answer is Boolean and Character because they do not extend the java.lang.Number class. The java.lang.Number class is the superclass of all the numeric wrapper classes in Java, such as Integer, Long, and Short, which means they extend the Number class. However, Boolean and Character are not numeric types and thus do not extend the Number class.
21.
What is default layout manager for panels and applets?
Correct Answer
A. Flowlayout
Explanation
The default layout manager for panels and applets is FlowLayout. FlowLayout arranges components in a left-to-right flow, wrapping them to the next line if there is not enough space horizontally. This layout manager is commonly used when you want components to be displayed in a simple, linear fashion.
22.
Which of the following statements are true?
Correct Answer(s)
A. When you construct an instance of File, if you do not use the filenaming semantics of the local machine, the constructor will throw an IOException.
B. When you construct an instance of File, if the corresponding file does not exist on the local file system, one will be created.
C. When an instance of File is garbage collected, the corresponding file on the local file system is deleted.
Explanation
The first statement is true because if the filenaming semantics of the local machine are not used when constructing an instance of File, the constructor will throw an IOException. The second statement is also true because if the corresponding file does not exist on the local file system, it will be created when constructing an instance of File. However, the third statement is false. When an instance of File is garbage collected, it does not automatically delete the corresponding file on the local file system.
23.
You execute the code below in an empty directory. What is the result?
File f1 = new File("dirname");
File f2 = new File(f1, "filename");
Correct Answer
E. No directory is created, and no file is created
Explanation
The code creates two File objects, f1 and f2. The constructor for f1 takes a string parameter "dirname", which represents a directory name. However, the code does not actually create a directory or a file. It simply creates the File objects with the given names. Therefore, the correct answer is that no directory is created, and no file is created.