ExamVeda
Login
Home
61
What will be the result of compiling and running the given code?
class A{
      int b=10;
      private A(){
            this.b=7;
      }
      int f(){
            return b;
      }
}
class B extends A{
      int b;
}
public class Test{
      public static void main(String[] args){
            A a = new B();
            System.out.println(a.f());
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

Choice A is the correct answer.

The code does not compile because the constructor of class A is declared as private. This creates a problem when the subclass constructor makes an implicit super() call to the parent class constructor at the time B is instantiated.

Since the code does not compile, all the other choices are incorrect. If the constructor of A had not been private, the output would have been 7.

62
Which component is responsible to run java program?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
63
Which of the below is not a Java Profiler?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
64
What is true about constructor?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
65
Which statement is true about java?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
66
What is not the use of "this" keyword in Java?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
67
When does method overloading is determined?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
68
Which of these operators is used to allocate memory for an object?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
69
What is true about Class.getInstance()?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
70
What will be the output of the following Java program?
class recursion 
{
    int func (int n) 
    {
        int result;
        result = func (n - 1);
        return result;
    }
} 
class Output 
{
    public static void main(String args[]) 
    {
        recursion obj = new recursion() ;
        System.out.print(obj.func(12));
    }
}
Discuss
Answer & Solution
Answer: Option D
Solution:
Let's break down why the answer is a Runtime Error.

The problem lies within the `func` method of the `recursion` class.
It's trying to use recursion, which means a function calling itself.

Here's the critical issue:
The `func` method calls itself (`func(n - 1)`) without a base case.
A base case is a condition that stops the recursive calls.

Without a base case, the function keeps calling itself again and again.

Imagine `func(12)` is called. It then calls `func(11)`, then `func(10)`, and so on.
This continues indefinitely, each call placing another "frame" onto the call stack (memory used to track function calls).

Eventually, the call stack runs out of space. This is called a StackOverflowError.
StackOverflowError is a type of Runtime Error.

Because the error happens while the program is running (not during compilation), it's a runtime error.

Therefore, the correct answer is Option D: Runtime Error.