1.
What will be printed as the output of the following program?
public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println(“I = ” +i);
}
}
Correct Answer
B. I=1
Explanation
The program initializes the variable i to 0. Then, it assigns the value of i++ (which is 0) to i, and adds the value of i (which is now 1) to it. So, the value of i becomes 1. Finally, it prints "I = 1" as the output.
2.
Which one of the following is not true?
Correct Answer
B. Abstract methods should be implemented in the derived class.
Explanation
The statement "Abstract methods should be implemented in the derived class" is not true. Abstract methods are declared in an abstract class but do not have an implementation. They are meant to be overridden and implemented in the derived class.
3.
What is the output of the following program:
public class testmeth
{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+” , “);
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
Correct Answer
C. 1,1
Explanation
The program outputs "1,1" because the variable "i" is not being modified within the method "m". The method "m" takes an integer parameter "i", but it only modifies the local variable "i" within the method. Therefore, the value of the static variable "i" remains unchanged at 1, resulting in the output "1,1".
4.
Which of the following is not true?
Correct Answer
C. An interface can implement another interface.
Explanation
The given statement that "An interface can implement another interface" is not true. In Java, interfaces can only extend other interfaces, not implement them. Implementation is done by classes, while interfaces provide a contract for classes to follow. Therefore, an interface can extend another interface to inherit its methods and add additional methods, but it cannot implement another interface.
5.
Which of the following is true?
Correct Answer
C. A finally block is executed whether an exception is thrown or not.
Explanation
A finally block is a block of code that is always executed, regardless of whether an exception is thrown or not. It is typically used to clean up resources or perform any necessary final actions. In this case, the given answer states that a finally block is executed whether an exception is thrown or not, which means that the code inside the finally block will always be executed, regardless of whether there is an exception or not.
6.
Consider the following code fragment
Rectangle r1 = new Rectangle();
r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);
After the above piece of code is executed, what are the colors of r1 and
r2 (in this order)?
Correct Answer
C. Color.red
Color.red
Explanation
The code creates a new Rectangle object called r1 and sets its color to blue. Then, a new reference variable called r2 is created and assigned to the same object as r1. Therefore, r1 and r2 both refer to the same object. When the color of r2 is set to red, it also changes the color of the object that r1 is referencing. As a result, both r1 and r2 have a color of red.
7.
What is the type and value of the following expression? (Notice the integer division)
-4 + 1/2 + 2*-3 + 5.0
Correct Answer
D. Double -5.0
Explanation
The expression involves a combination of integers and a floating-point number. The division operation is performed first, resulting in 1/2 being evaluated as 0.5. The rest of the expression is then evaluated according to the order of operations, with the multiplication operation (-3 * 2) resulting in -6. Finally, the addition and subtraction operations are performed, resulting in -4 + 0.5 - 6 + 5.0 = -4.5. Since the expression includes a floating-point number (5.0), the overall type of the expression is double. Therefore, the correct answer is double -5.0.
8.
What is printed by the following statement?
System.out.print(“Hello,\nworld!”);
Correct Answer
C. Hello, world!
Explanation
The given statement will print "Hello, world!" to the console.
9.
Consider the two methods (within the same class)
public static int foo(int a, String s)
{
s = “Yellow”;
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = “Blue”;
a = foo(a,s);
System.out.println(“a=”+a+” s=”+s);
}
public static void main(String args[])
{
bar();
}
What is printed on execution of these methods?
Correct Answer
D. A = 5 s = Blue
Explanation
The method foo takes an integer and a string as parameters. Inside the method, the string parameter "s" is assigned the value "Yellow". The integer parameter "a" is incremented by 2. The method then returns the updated value of "a". In the method bar, an integer variable "a" is initialized with the value 3 and a string variable "s" is initialized with the value "Blue". The method foo is called with the variables "a" and "s" as arguments. The value returned by foo, which is 5, is assigned to "a". Finally, the values of "a" and "s" are printed, which are 5 and "Blue" respectively.
10.
Consider the following class definition:
public class MyClass
{
private int value;
public void setValue(int i){ / code / }
// Other methods…
}
The method setValue assigns the value of i to the instance field value. What could you write for the implementation of setValue?
Correct Answer
D. (D) Both (A) and (B) and above
Explanation
The correct answer is (D) Both (A) and (B) and above. This is because both options (A) and (B) correctly assign the value of i to the instance field value. Option (A) directly assigns the value, while option (B) uses the "this" keyword to refer to the current object and then assigns the value. Both implementations achieve the same result.
11.
Which of the following is TRUE?
Correct Answer
D. A class has always a constructor (possibly automatically supplied by the java compiler).
Explanation
A class has always a constructor (possibly automatically supplied by the java compiler). In Java, a constructor is a special method that is used to initialize objects of a class. If a class does not explicitly define a constructor, the Java compiler automatically provides a default constructor. Therefore, every class in Java has a constructor, either defined explicitly or provided by the compiler.
12.
Consider,
public class MyClass
{
public MyClass(){/code/}
// more code…
}
To instantiate MyClass, you would write?
Correct Answer
A. MyClass mc = new MyClass();
Explanation
To instantiate the MyClass class, you would write "MyClass mc = new MyClass();". This is the correct syntax for creating a new instance of a class in Java. The "new" keyword is used to allocate memory for the object, and the constructor "MyClass()" is called to initialize the object. The resulting object is then assigned to the variable "mc".
13.
You read the following statement in a Java program that compiles and executes.
submarine.dive(depth);
What can you say for sure?
Correct Answer
B. Dive must be a method.
Explanation
Based on the given statement, "submarine.dive(depth)", we can say for sure that "dive" must be a method. This is because the statement is using the dot operator to call the "dive" method on the "submarine" object. If "dive" was not a method, this statement would result in a compilation error. However, we cannot determine the data type of "depth" or whether "submarine" is the name of a class without further information.
14.
The correct order of the declarations in a Java program is,
Correct Answer
A. Package declaration, import statement, class declaration
Explanation
The correct order of declarations in a Java program is package declaration, import statement, and then class declaration. This is because the package declaration specifies the package in which the class belongs, the import statement is used to import necessary classes or packages, and finally, the class declaration defines the class itself.
15.
A protected member can be accessed in,
Correct Answer
C. A non-subclass of different
Explanation
A protected member can be accessed by a non-subclass of a different package because the protected access modifier allows access to the member within the same package and also by any subclass, regardless of whether it is in the same package or a different package.
16.
What is the output of the following code:
class eq
{
public static void main(String args[])
{
String s1 = “Hello”;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}
Correct Answer
B. False
Explanation
The output of the code is "false". This is because the "==" operator in Java checks for reference equality, meaning it compares whether two variables refer to the same object in memory. In this case, although the content of s1 and s2 is the same ("Hello"), they are two different String objects because s2 is created using the "new" keyword. Therefore, s1 and s2 have different memory addresses, resulting in the "==" comparison returning false.
17.
All exception types are subclasses of the built-in class
Correct Answer
D. Throwable
Explanation
All exception types in Java are subclasses of the built-in class "Throwable". This means that any exception that occurs during the execution of a Java program can be caught and handled using the "Throwable" class. The "Throwable" class is the superclass of all exception classes, including "Exception", "RuntimeExceptions", and "Error". Therefore, the statement is correct in stating that all exception types are subclasses of "Throwable".
18.
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the
Correct Answer
B. Subclass
Explanation
When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. This is because when a method is overridden, the subclass provides its own implementation of the method, which replaces the implementation in the superclass. Therefore, when the method is called from within the subclass, the subclass's implementation will be executed.
19.
What will be the output of the following program?
- public class MyFirst {
- public static void main(String[] args) {
- MyFirst obj = new MyFirst(n);
- }
- static int a = 10;
- static int n;
- int b = 5;
- int c;
- public MyFirst(int m) {
- System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
- }
- // Instance Block
- {
- b = 30;
- n = 20;
- }
- // Static Block
- static
- {
- a = 60;
- }
- }
Correct Answer
D. 60, 30, 0, 20, 0
Explanation
The output of the program will be "60, 30, 0, 20, 0". This is because the static block is executed before the main method, so the value of "a" is changed to 60. Then, the instance block is executed before the constructor, so the values of "b" and "n" are changed to 30 and 20 respectively. Finally, the constructor is called with the parameter "n", and it prints the values of "a", "b", "c", "n", and "m".
20.
What does the expression float a = 35 / 0 return?
Correct Answer
D. Runtime exception
Explanation
The expression float a = 35 / 0 returns a runtime exception. This is because dividing any number by zero is undefined in mathematics and programming. It violates the fundamental rule of division and leads to an error known as "ArithmeticException" in programming languages. Therefore, attempting to divide 35 by 0 will result in a runtime exception being thrown.
21.
Use the following declaration and initialization to evaluate the Java expressions
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
System.out.println( – -b * a + c *d – -);
Correct Answer
B. 24
Explanation
The given expression can be evaluated as follows:
-(-b * a + c * d) - (-)
= -( -3 * 2 + 4 * 5) - (-)
= -( -6 + 20) - (-)
= -(14) - (-)
= -14 - (-)
= -14 + 0
= -14
Therefore, the correct answer is 24.
22.
Identify, from among the following, the incorrect variable name(s).
Correct Answer
C. 2ndName
Explanation
The variable name "2ndName" is incorrect because variable names cannot start with a number. Variable names must start with a letter or an underscore.
23.
Use the following declaration and initialization to evaluate the Java expressions
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
System.out.println (-2U * ( g – k ) +c);
Correct Answer
A. Syntax Error
24.
Consider the following Java program :
class IfStatement{
public static void main(String args[])
{
int a=2, b=3;
if (a==3)
if (b==3)
System.out.println(“===============”);
else
System.out.println(“#################”);
System.out.println(“&&&&&&&&&&&”);
}
}
Which of the following will the output be?
Correct Answer
C. &&&&&&&&&&&
Explanation
The output will be "&&&&&&&&&". This is because the if statement checks if "a" is equal to 3, which is false. Therefore, the inner if statement is not executed and the else statement is skipped. As a result, only the last System.out.println statement is executed, which prints "&&&&&&&&&".
25.
Consider the following program:
class prob1{
int puzzel(int n){
int result;
if (n==1)
return 1;
result = puzzel(n-1) * n;
return result;
}
}
class prob2{
public static void main(String args[])
{
prob1 f = new prob1();
System.out.println(” puzzel of 6 is = ” + f.puzzel(6));
}
}
Which of the following will be the output of the above program?
Correct Answer
D. 720
Explanation
The program defines a class called prob1 with a method called puzzel. The puzzel method takes an integer n as input and recursively calculates the factorial of n. In the main method of the prob2 class, an instance of prob1 is created and the puzzel method is called with an input of 6. The output of the program will be 720, which is the factorial of 6.
26.
What will be the output of the following program?
- public class Test {
- public static void main(String[] args) {
- int count = 1;
- while (count <= 15) {
- System.out.println(count % 2 == 1 ? "***" : "+++++");
- ++count;
- } // end while
- } // end main
- }
Correct Answer
C. 8 times *** and 7 times +++++
Explanation
The program uses a while loop to iterate from 1 to 15. Inside the loop, it checks if the current count is odd or even using the expression "count % 2 == 1". If the count is odd, it prints "***", otherwise it prints "+++++". Since there are 8 odd numbers and 7 even numbers between 1 and 15, the output will be 8 times "***" and 7 times "+++++".
27.
What will be the output of the following program?
- public class Test2 {
- public static void main(String[] args) {
- StringBuffer s1 = new StringBuffer("Complete");
- s1.setCharAt(1,'i');
- s1.setCharAt(7,'d');
- System.out.println(s1);
- }
- }
Correct Answer
C. Cimpletd
Explanation
The program creates a StringBuffer object with the initial value "Complete". The setCharAt() method is then used to replace the character at index 1 with 'i' and the character at index 7 with 'd'. Therefore, the output will be "Cimpletd".
28.
Given,
- int values[ ] = {1,2,3,4,5,6,7,8,9,10};
- for(int i=0;i< Y; ++i)
- System.out.println(values[i]);
Find the value of value[i]?
Correct Answer
D. None of These
29.
Class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println("Got the Exception " + e);
}
}
}
Find the output of the code.
Correct Answer
C. Compiler error
Explanation
The given code will result in a compiler error because the code is attempting to throw an integer value (10) instead of an exception object. The catch block is expecting an exception object to handle, not an integer value.
30.
Predict the output of the following program.
class Test
{
String str = "a";
void A()
{
try
{
str +="b";
B();
}
catch (Exception e)
{
str += "c";
}
}
void B() throws Exception
{
try
{
str += "d";
C();
}
catch(Exception e)
{
throw new Exception();
}
finally
{
str += "e";
}
str += "f";
}
void C() throws Exception
{
throw new Exception();
}
void display()
{
System.out.println(str);
}
public static void main(String[] args)
{
Test object = new Test();
object.A();
object.display();
}
}
Correct Answer
D. Abdec
Explanation
The program starts by creating an object of the Test class and calling the method A(). In method A(), it tries to concatenate "b" to the string "str" and then calls method B(). In method B(), it tries to concatenate "d" to the string "str" and then calls method C(). In method C(), it throws an exception. Since method C() is called within a try block in method B(), the catch block is executed. In the catch block, "c" is concatenated to the string "str". Finally, "e" is concatenated to the string "str" and then "f" is concatenated outside the try-catch block in method B(). Finally, the display() method is called and it prints the value of "str", which is "abdec".