1.
What is java ?
Correct Answer
A. Oop language
Explanation
Java is an object-oriented programming (OOP) language. It is designed to follow the principles of OOP, which include encapsulation, inheritance, and polymorphism. Java allows developers to create modular and reusable code by creating objects and classes. It provides features like inheritance, polymorphism, and abstraction, which make it easier to design and implement complex software systems. Java is widely used for developing various types of applications, including desktop, web, and mobile applications.
2.
Which of these is a keyword in java?
Correct Answer(s)
A. Goto
B. Enum
C. Switch
D. Class
Explanation
In Java, "enum", "switch", and "class" are all keywords. Keywords are reserved words that have a specific meaning in the programming language and cannot be used as identifiers. "goto" and "critical" are not keywords in Java.
3.
Which is legal identifier name?
Correct Answer(s)
A. Int __;
B. Int $_1;
E. Int th_is;
Explanation
The legal identifier names in this question are "int __;", "int $_1;", and "int th_is;". The first two options use underscores and dollar signs, which are allowed characters in identifier names. The third option uses a combination of letters and underscores, which is also a valid identifier name.
4.
"Tiger" is used to denote which version of java?
Correct Answer
A. Java 5.0
Explanation
"Tiger" is a code name used to refer to Java 5.0. Code names are often used by software developers to refer to different versions of their software during development. In the case of Java 5.0, it was given the code name "Tiger" before its official release. Therefore, "Tiger" is used to denote Java 5.0.
5.
Which one of these is primitive datatype in java?
Correct Answer(s)
A. Int
B. Boolean
E. Float
Explanation
The primitive data types in Java are int, boolean, and float. These data types are considered primitive because they are not objects and do not have any methods or properties associated with them. They are basic building blocks in Java and are used to store simple values such as integers, true/false values, and decimal numbers. Short and Long are not considered primitive data types in Java as they are wrapper classes for the primitive data types short and long respectively.
6.
Try{int x=Integer.parseInt("two");}what type of exceptions can be caugth in catch block?
Correct Answer(s)
B. IllegalArgumentException
C. NumberFormatException
Explanation
The code snippet is attempting to convert the string "two" into an integer using the parseInt() method. However, since "two" cannot be converted into a valid integer, it will throw a NumberFormatException. Additionally, since the code does not handle any other exceptions specifically, any other exception that may occur during the execution of the code will be caught by the catch block as a general Exception. Therefore, the correct answer is IllegalArgumentException and NumberFormatException.
7.
Class A{static void sing(){System.out.print("fa");}}class B extends A{static void sing(){System.out.print("la";)}public static void main(String[] args){A one=new B();B two=new B();System.out.print(one.sing()+""+two.sing());}}}what is the result?
Correct Answer
A. Fa la
Explanation
The code defines two classes, A and B. Class B extends class A. Both classes have a static method called "sing" that prints out a string. In the main method, two objects are created, one of type A and one of type B. The "sing" method of class A is called on the object of type A, which prints "fa". The "sing" method of class B is called on the object of type B, which prints "la". Therefore, the output of the code is "fa la".
8.
Class Top{Top(String s){System.out.println("Hello");}}class Bottom extends Top{Bottom(String t){}public static void main(String[] args){new Bottom("BIT"); }}what is the result?
Correct Answer
D. Compilation fail
Explanation
The given code will result in a compilation fail. This is because the class Bottom is extending the class Top, but it does not have a constructor that calls the constructor of the superclass. Since the superclass constructor is not explicitly called in the subclass constructor, the compiler will generate a default constructor for the subclass. However, the default constructor in the superclass requires a String parameter, so the compiler will not be able to generate a default constructor for the subclass, resulting in a compilation error.
9.
Class Alpha{int over=1;}class Beta extends Alpha{int over=2; }class Gamma extends Beta{int over=3;public static void main(String[] args){new Gamma().go();}void go(){Beta b=new Gamma();Alpha a=new Gamma();System.out.println(super.over+""+b.over+""+a.over);}}what is the result?
Correct Answer
A. 221
Explanation
The program creates an object of the Gamma class and calls the go() method. Inside the go() method, a Beta object is created and assigned to the variable 'b', and an Alpha object is created and assigned to the variable 'a'.
The 'super.over' expression inside the println statement refers to the 'over' variable in the parent class Beta, which has a value of 2. The 'b.over' expression refers to the 'over' variable in the Beta class, which has a value of 2. The 'a.over' expression refers to the 'over' variable in the Alpha class, which has a value of 1.
Therefore, the output will be "221".
10.
Public class Switch2{public static short x=1;staticfinal int y=3;public static void main(String[] args){for(int z=3;z>0;--z){switch(z){case y:System.out.print("0");case y-1:System.out.print("1");case x:z--;}}}}what is the result?
Correct Answer
E. Compilation fails at line 9
Explanation
The compilation fails at line 9 because the variable "x" is not a constant expression. In a switch statement, the cases must be constant expressions, but "x" is a variable and its value can change. Therefore, the code will not compile.
11.
Class Zeta{public static void main(String[] args){int x=1;if((4>x)^((++x + 2)>3)) x++;if((4 > ++x)^(++x==5)) x++;System.out.print(x);}}what is the result?
Correct Answer
C. 4
Explanation
In this code, the variable x is initially assigned a value of 1. The first if statement checks if (4 > x) is true, which is false because x is 1. The second part of the if statement is ((++x + 2) > 3), which is true because x is incremented to 2 and 2 + 2 is greater than 3. Since the first part of the if statement is false and the second part is true, the XOR operator (^) returns true. Therefore, x is incremented to 2. The second if statement checks if (4 > ++x) is true, which is false because x is now 3. The second part of the if statement is (++x == 5), which is false because x is incremented to 4 and 4 is not equal to 5. Since both parts of the if statement are false, the XOR operator returns false. Therefore, x remains 3. Finally, the value of x (which is 3) is printed.
12.
Which are true?
Correct Answer(s)
B. If a class has synchronized code,multiple threads can still access the non-synchronized code.
E. When a thread invockes wait(),it releases its locks
Explanation
When a class has synchronized code, multiple threads can still access the non-synchronized code. This means that even if some parts of the class are synchronized and only one thread can access them at a time, other threads can still access the non-synchronized parts simultaneously. This can lead to potential concurrency issues and unexpected behavior.
When a thread invokes the wait() method, it releases its locks. This means that when a thread is waiting for a certain condition to be met, it releases any locks it holds, allowing other threads to acquire those locks and continue their execution. This is important for proper synchronization and coordination between multiple threads.
13.
Public class MyProgram{public static void main(String[] args){try{ System.out.print("Hello World");}finally{System.out.println("Finally executing");}}}what is the result?
Correct Answer
D. Hello world Finally executing
Explanation
The program will print "Hello World" and then execute the code in the finally block, which will print "Finally executing". Therefore, the result will be "Hello World Finally executing".
14.
Class Alpha{int doStuff(float b){return 7;}}class Beta extends Alha{//insert code here}which,inserted independently at line 8,will compile?
Correct Answer(s)
B. Protected int doStuff(float y){return 4;}
D. Final int doStuff(float y){return 4;}
Explanation
The given code is creating two classes, Alpha and Beta. Beta is extending Alpha, indicating that Beta is a subclass of Alpha. The question asks which code, when inserted at line 8 in class Beta, will compile successfully.
The correct answer is "protected int doStuff(float y){return 4;}, final int doStuff(float y){return 4;}".
This answer is correct because it provides two valid methods that override the method "doStuff" from the superclass Alpha. The "protected" access modifier allows the method to be accessed within the package and by subclasses, while the "final" keyword indicates that the method cannot be overridden by any subclasses.
15.
Class Silky extends Smooth{int x=5;int y=7;public static void main(String[] args){new Silky().go();}void go(){if(x>y & (Boolean)(this instanceof Silky)) System.out.print("a");if(Long.valueOf(4) instanceof Number) System.out.print("b");}}class Smooth{}what is the result?
Correct Answer
B. B
Explanation
The code will print "b" as the result. This is because the first if statement evaluates to true since both conditions are true: x is greater than y, and the instance of the object is of type Silky. The second if statement also evaluates to true since Long.valueOf(4) is an instance of Number. Therefore, both if statements will execute and print "b".
16.
Which are true?
Correct Answer(s)
A. Encapsulation limits the consequences of change.
B. Encapsulation makes it easier to reuse classes.
C. Encapsulation results in better testing and higher reliability.
Explanation
Encapsulation limits the consequences of change by hiding the internal implementation details of a class, allowing changes to be made to the class without affecting other parts of the program. This helps to maintain the integrity of the code and reduces the risk of introducing bugs or errors.
Encapsulation makes it easier to reuse classes by bundling the data and methods that operate on the data within a single unit. This allows the class to be used as a black box, where the internal workings are hidden and only the interface is exposed. This simplifies the process of reusing the class in different parts of the program.
Encapsulation results in better testing and higher reliability because it promotes modular design and reduces dependencies between different parts of the program. This makes it easier to test individual components in isolation, leading to more effective and efficient testing. Additionally, encapsulation helps to prevent unintended access or modification of data, improving the reliability of the code.
17.
Class X{void go(){System.out.print("x");}}class Y extends X{void go(){System.out.print("y");}}class Z extends X{void go(){System.out.print("z");}}class Chrome1{public static void main(String[] args){X z=new Z();X y=new Y();Z y2=(Z)y;y.go();y2.go();}}what is the result?
Correct Answer
E. An exception is thrown at runtime
Explanation
The code creates an object of class Z and assigns it to a variable of type X. Then, it creates an object of class Y and assigns it to another variable of type X. Next, it tries to cast the Y object to type Z and assigns it to a variable of type Z. However, since the Y object is not actually an instance of Z, a ClassCastException is thrown at runtime. Therefore, the correct answer is "An exception is thrown at runtime".
18.
Public class Felix{ protected long cat(){return 7L;}}class Oscar extends Felix{//insert code here}
which method inserted ,will compile?
Correct Answer
C. Long cat(){return 7;}
Explanation
The method "long cat(){return 7;}" will compile because it has the same return type and access modifier as the method in the superclass Felix.
19.
Which of the following are legal definitions for non-nested classes and interfaces?
Correct Answer(s)
B. Abstract public class Foo{}
E. Final public class Foo{}
Explanation
The correct answer is "abstract public class Foo{}, final public class Foo{}".br/>The first option, "static public interface Foo{}", is incorrect because interfaces cannot be declared as static. The second option, "abstract public class Foo{}", is correct because classes can be declared as abstract and public. The third option, "protected interface Foo{}", is incorrect because interfaces cannot be declared with access modifiers other than public. The fourth option, "final abstract class Foo{}", is incorrect because a class cannot be both abstract and final. The fifth option, "final public class Foo{}", is correct because classes can be declared as final and public.
20.
Public class Switch2{public static void main(String[] args){int y=0;for(int x=3;x>=0;x--){switch(x){case 3:y=y+100;case 2:y=y+10;case 1:break;case 0:y=y+1;}}System.out.println(y);}}what is the result?
Correct Answer
B. 121
Explanation
The code starts with the variable y initialized to 0. Then, a for loop is executed 4 times, with x starting at 3 and decreasing by 1 in each iteration. Inside the loop, a switch statement is used to check the value of x. When x is 3, y is increased by 100. When x is 2, y is increased by 10. When x is 1, the break statement is encountered, causing the switch statement to exit. When x is 0, y is increased by 1. Therefore, the final value of y is 121.
21.
Public static void main(String[] args){TreeSet<Number> tree=new TreeSet<Number>();tree.add(108);//insert here}which is inserted to compile and run
Correct Answer(s)
C. Tree.add(Integer.valueOf("-1"));
E. Tree.add(0xCAFE)
Explanation
The given code is trying to insert elements into a TreeSet of type Number. The TreeSet is a sorted set, which means it only allows elements that can be compared to each other.
Out of the given options, "tree.add(Integer.valueOf("-1"))" and "tree.add(0xCAFE)" are the correct answers because they both insert valid Number objects into the TreeSet.
The first option inserts an Integer object with a value of -1, which is a valid number. The second option inserts an Integer object with a hexadecimal value of 0xCAFE, which is also a valid number.
The other options are incorrect because they try to insert a double value, a string, and a null value, which are not valid Number objects and cannot be compared in a TreeSet.
22.
Calss Bulbs{ enum Turn{ ON("bright"),OFF("dark");}public staticmvoid main(String[] args){ System.out.println(Turn.ON);}}what is the result?
Correct Answer
C. Compilation fails
Explanation
The code provided will not compile because there is a typo in the main method declaration. "public staticmvoid" should be "public static void". As a result, the code will throw a compilation error.
23.
Which is true?
Correct Answer(s)
C. Use assertions to verify the arguments of private methods.
D. Assertions can be disabled for a particular class
Explanation
Assertions are typically used to verify assumptions about the code during development and testing. By using assertions to verify the arguments of private methods, developers can ensure that the inputs to these methods are valid and meet the expected criteria. This helps catch potential bugs and errors early on. Additionally, assertions can be disabled for a particular class, which allows them to be selectively enabled or disabled based on the specific needs of the codebase or application. This flexibility can be useful in different testing scenarios or production environments.
24.
Class Battery{static int x=1;public static void main(String[] args) throws Throwable{try{if(x==1)throw new Throwable();System.out.println("try");}catch(Exception e){System.out.println("exc");}finally{System.out.println("fin");}}}what is the result?
Correct Answer
D. Fin then a runtime exception
Explanation
The code first checks if the value of x is equal to 1. Since it is, it throws a new Throwable object. This causes the program to skip the "try" block and go directly to the "catch" block. The catch block prints "exc". After the catch block, the "finally" block is executed, which prints "fin". Since there is no code to handle the thrown Throwable object, a runtime exception occurs after the "finally" block is executed. Therefore, the result is "fin then a runtime exception".
25.
Which of the following can be constructed using a String?
Correct Answer(s)
A. Java.io.File
B. Java.io.FileWriter
D. Java.io.FileReader
Explanation
A String can be used to construct java.io.File, java.io.FileWriter, and java.io.FileReader objects. These classes are part of the Java I/O library and are used for file handling operations. A String can be passed as an argument to the constructors of these classes to specify the file path or name. The java.io.BufferedWriter and java.io.PrintWriter classes do not have constructors that accept a String as a parameter, so they cannot be constructed using a String.
26.
String sa ="ankur ";
Correct Answer
B. Sa is object.
Explanation
The given code declares a string variable named "sa" and assigns it the value "ankur". In Java, a string is an object of the String class. Therefore, "sa" is an object of the String class.
27.
Constructor can have a return type as ?
Correct Answer
E. None of them.
Explanation
The correct answer is "none of them" because a constructor is a special method used to initialize objects of a class. It does not have a return type, as its purpose is to set up the initial state of an object rather than returning a value. Therefore, the return type cannot be specified for a constructor in any programming language.
28.
Java having pointers as ?
Correct Answer
E. None of them.
Explanation
The correct answer is "none of them" because Java does not have pointers like other programming languages such as C or C++. Instead, Java uses references to objects, which are similar to pointers but with additional safety measures. In Java, variables of primitive types (such as int or float) store the actual values, while variables of object types (such as String or Object) store references to the memory location where the object is stored. Therefore, Java does not have pointers in the traditional sense.
29.
Which type of inharitence java used ?
Correct Answer(s)
B. Single level.
C. Multi level.
D. Hibrid .
Explanation
Java uses single level, multi level, and hybrid inheritance.
Single level inheritance occurs when a class inherits from only one superclass. In Java, a class can extend only one superclass.
Multi level inheritance occurs when a class inherits from a superclass, and that superclass itself inherits from another superclass. This creates a hierarchy of classes.
Hybrid inheritance is a combination of different types of inheritance, such as single level and multi level inheritance, in a single program.
Therefore, Java can use all three types of inheritance: single level, multi level, and hybrid.
30.
Java is not ?
Correct Answer
E. Structural.
Explanation
The term "structural" refers to the organization and arrangement of code in a program. In the context of Java, it means that the language provides features and constructs that allow developers to create well-organized and modular code. Java supports object-oriented programming principles, such as classes, objects, and inheritance, which enable developers to create a clear and logical structure for their code. This helps in improving code readability, maintainability, and reusability. Therefore, the correct answer is "structural" because Java provides mechanisms for structuring code effectively.