1.
Which of the following statement is true regarding constructors?
Which of the following statement is true regarding constructors?
Correct Answer
A. Default Constructors are optional only for the classes that does not have constructors
Explanation
Default constructors are optional only for the classes that do not have constructors because if a class does not have any constructor defined, the compiler automatically provides a default constructor for that class. However, if a class already has a constructor defined, the default constructor is not automatically provided and must be explicitly defined if needed.
2.
Consider the following code: public abstract class Shape { private int x; private int y; public abstract void draw(); public void setAnchor(int x, int y) { this.x = x; this.y = y; } }
Correct Answer(s)
C. Public class Circle extends Shape { private int radius; public void setRadius(int radius) { this.radius = radius; } public int getRadius() { return radius; } public void draw() {/* code here */}}
E. Public class Circle extends Shape { public int radius;
private void draw() {/* code here */} }
3.
Which of the following options is true about multiple inheritance?
Correct Answer
A. Inheriting from two super classes
Explanation
Multiple inheritance refers to the ability of a class to inherit attributes and behaviors from more than one parent class. In this case, the correct answer states that multiple inheritance involves inheriting from two super classes. This means that a class can inherit attributes and behaviors from two different parent classes simultaneously.
4.
Consider the following code: import java.util.*; public class Code10 { { final Vector v; v=new Vector(); } public Code10() { } public void codeMethod() { System.out.println(v.isEmpty()); } public static void main(String args[]) { new Code10().codeMethod(); } }
Correct Answer
C. Compilation error: cannot find the symbol
Explanation
The correct answer is "Compilation error: cannot find the symbol". This is because the variable "v" is declared inside the constructor and is not accessible outside the constructor. Therefore, in the codeMethod() method, when trying to access the "v" variable, it cannot be found and a compilation error occurs.
5.
Consider the following code: public class LabeledBreak2 { public static void main(String args[]) { loop: for(int j=0; j<2; j++) { for(int i=0; i<10; i++) { if(i == 5) break loop; System.out.print(i + " "); } } } }
Which of the following will be the output for the above code?
Correct Answer
D. D. 0 1 2 3 4
Explanation
The code uses a labeled break statement "break loop" to break out of the outer loop when i equals 5. This means that the inner loop will only run until i equals 5, and the outer loop will only run once. Therefore, the output will be 0 1 2 3 4.
6.
Consider the following scenario: Real Chocos Private Limited deals in manufacturing variety of chocolates. This organization manufactures three varieties of chocolates. 1. Fruit Chocolates 2. Rum Chocolates 3. Milk Chocolates A software system needs to be built. Which of the following options identifies the Classes and Objects?
Correct Answer
C. C. Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates
Explanation
In this scenario, the class represents the general category of "Chocolate" and the objects represent the specific varieties of chocolates that can be manufactured, which are "Fruit Chocolates", "Rum Chocolates", and "Milk Chocolates". Therefore, option c correctly identifies the classes and objects in this scenario.
7.
Consider the following partial code: interface A { public int getValue(); } class B implements A { public int getValue() { return 1; } } class C extends B { // insert code here } Which of the following code fragments, when inserted individually at the commented line (// insert code here), makes use of polymorphism? (Choose 3)
Correct Answer(s)
A. A. public void add(B b) { b.getValue(); }
B. B. public void add(A a) { a.getValue(); }
E. E. public void add(A a, B b) { a.getValue(); }
Explanation
The correct answer choices (a, b, and e) all involve methods that accept objects of different classes as parameters. This demonstrates polymorphism because objects of class B and class C are both subclasses of class A, so they can be treated as objects of type A. By accepting objects of type A or B as parameters, these methods can be used with objects of class B or class C, showing polymorphic behavior.
8.
Which of the following are correct regarding HashCode?(Choose 2)
Correct Answer(s)
D. D. It improves performance
E. E. hashCode() is defined in String class
Explanation
The given answer states that HashCode improves performance and that hashCode() is defined in the String class. HashCode is a 32-bit numeric digest key that is used to improve performance in various data structures like hash tables. The hashCode() method is indeed defined in the String class and is used to generate the hash code value for a given string. The answer does not mention anything about the numeric key being unique or the hashCode() value not being a zero-value.
9.
Which are all platform independent among the following? (Choose 3)
Correct Answer(s)
B. B. Java Source Files
D. D. Java Class Files
E. E. JAR Files
Explanation
Java Source Files, Java Class Files, and JAR Files are all platform independent. Java Source Files are written in plain text and can be compiled and executed on any platform that has a Java Virtual Machine (JVM) installed. Java Class Files are generated by the Java compiler and contain bytecode that can be interpreted by the JVM, making them platform independent. JAR Files are archives that contain compiled Java classes and resources, and they can be run on any platform with a JVM. The JVM itself is not platform independent as it needs to be specifically implemented for each operating system.
10.
Consider the following listed items:
A. a method declared as final
B. a method declared as abstract
C. a method declared as private
Consider the following statements:
I. Will not be available in sub classes
II. Will deny overriding the method
III. Will not allow instantiating the class
Which of the following option gives the exact matches of above listed items and statements?
Correct Answer
A. A-II, B-III, C-I
Explanation
Option A-II, B-III, C-I gives the exact matches of the listed items and statements. This means that a method declared as final will deny overriding the method, a method declared as abstract will not allow instantiating the class, and a method declared as private will not be available in sub classes.
11.
Consider the following code:
public class TestOverloading {
int _length(String s) {
return s.length();
}
float _length(String s) {
return (float) s.length();
}
}
Which of the following statement is true regarding the above code?
Correct Answer
A. Both the length() methods are duplicated methods
12.
Consider the following code:
class AllClass {
private static int i = 10;
static { i += 10; }
{ i += 10; }
AllClass() { i += 10; }
AllClass incrementWith10() { i += 10; return this;}
}
public class AllAccess {
public static void main(String[] args) {
System.out.println(new AllClass().incrementWith10().i);
}
}
Which of the following option gives the output for the above code?
Correct Answer
A. Compile time error
Explanation
The code will result in a compile time error because the variable "i" is declared as private in the AllClass class. This means that it can only be accessed within the same class and cannot be accessed in the main method of the AllAccess class. Therefore, the code will not compile and will result in a compile time error.
13.
Which of the following options give the valid argument types for main() method?
1) String[] args
2) String args[]
3) String ..args
4) String args
5) String[] args[]
Correct Answer
A. 1,2,3
Explanation
The valid argument types for the main() method in Java are 1) String[] args, 2) String args[], and 3) String ..args. These options correctly represent the different ways in which the command-line arguments can be passed to the main() method. Option 1 uses the standard array syntax, option 2 uses an alternative array syntax, and option 3 uses varargs syntax to allow for a variable number of arguments.
14.
Which of the following option gives one possible use of the statement 'the name of the public class should match with its file name'?
Correct Answer
A. Helps the compiler to find the source file that corresponds to a class, when it does not find a class file while compiling
Explanation
The statement "the name of the public class should match with its file name" helps the compiler to find the source file that corresponds to a class when it does not find a class file while compiling. This ensures that the compiler can locate the correct file and compile the class successfully.
15.
Which of the following statement is true?
Correct Answer
A. Classes can be loaded at Runtime, without actually referring the class in the code at compile time.
Explanation
Classes can indeed be loaded at runtime without actually referring to the class in the code at compile time. This is possible through the use of mechanisms such as reflection in Java. Reflection allows for the dynamic loading and instantiation of classes at runtime, enabling the program to access and utilize classes that were not known or referenced during compilation. This flexibility is particularly useful in scenarios where the specific classes to be used may vary or change dynamically during the execution of the program.
16.
Consider the following code:
interface Declare {
Declaration 1: protected int a = 5;
Declaration 2: public static final int e = 9;
Declaration 3: volatile int c = 7;
Declaration 4: transient int d = 8;
}
Which of the following option gives the declarations that results in compilation error?
Correct Answer
A. Declaration 1,3,4
Explanation
Declaration 1,3,4 will result in compilation error because the access modifiers used (protected and volatile) are not allowed for variables in an interface. Interface variables are by default public, static, and final. Therefore, using protected and volatile for Declaration 1 and Declaration 3 respectively will cause a compilation error. Additionally, using transient for Declaration 4 is also not allowed in an interface, resulting in a compilation error.
17.
Consider the following Statements:
Statement A: Anonymous inner class can be created in initializer or static blocks
Statement B: Anonymous inner class has no constructor
Which of the following option is true regarding the above given statements?
Correct Answer
A. Both Statements A and B are true
Explanation
Both statements A and B are true. An anonymous inner class can be created in an initializer or static block, as well as in other places such as method arguments or expressions. Additionally, an anonymous inner class does not have a constructor because it is created and instantiated at the same time.
18.
Consider the following code:
Line 1:class A {
Line 2: void display() { }
Line 3:}
Line 4:class B extends A {
Line 5: // insert missing code here
Line 6:}
Which of the following options give the code snippets, when inserted individually at the line no 5, will correctly complete the definition of class B?
1) int display() { /* more code here */ }
2) void display() { /* more code here */ }
3) private void display() { /* more code here */ }
4) protected void display() { /* more code here */ }
Correct Answer
A. 2,4
Explanation
The correct answer is options 2 and 4. This is because class B is extending class A, which means it is inheriting all the methods and variables of class A. Therefore, in order to correctly complete the definition of class B, the missing code at line 5 should be the same method signature as the one in class A. Option 2, which is "void display() { /* more code here */ }", matches the method signature in class A. Option 4, which is "protected void display() { /* more code here */ }", also matches the method signature in class A and is a valid option.
19.
consider the following code:
Line No:1 public class MovieRelease
Line No:2 {
Line No:3 public static void main(String[] args) {
Line No:4 class Movie {
Line No:5 public String name;
Line No:6 public Movie(String s) {
Line No:7 name = s;
Line No:8}}
Line No:9 Object obj = new Movie("MaskOfZoro");
Line No:10 System.out.println(obj.name);
Line No:11} }
Which of the following option gives the valid output for the above code?
Correct Answer
A. Zippo
Explanation
The code defines a class Movie with a constructor that takes a String parameter and assigns it to the instance variable name. In the main method, an object of the Movie class is created with the name "MaskOfZoro" and assigned to the variable obj. Finally, the name of the object is printed. Since the name of the object is "MaskOfZoro", the output will be "MaskOfZoro". Therefore, the valid output for the above code is "Zippo".
20.
Consider the following code:
Line no 1:class Outer {
Line no 2:public static class Inner {
Line no 3:}
Line no 4:public static void display() { } }
Line no 5:public class Test
Line no 6:{
Line no 7:public static void main(String args[])
Line no 8:{
Line no 9:// Replace with code from the option below
Line no 10:}}
Which of the following option when replaced at line no 9, instantiates an instance of the nested class?
Correct Answer
A. Outer.Inner o = new Outer.Inner();
Explanation
The correct answer is "Outer.Inner o = new Outer.Inner();". This code instantiates an instance of the nested class "Inner" by using the syntax "Outer.Inner" to access the nested class and then using the "new" keyword to create a new instance of it. The instance is then assigned to the variable "o".