Java Programming Questions and Answers Set 11

Java Programming OOPs

Questions 101 to 110



101.
Given the following source code, which comment line can be uncommented without introducing errors?
abstract class MyClass
{
   abstract void f();
   final void g() {}
// final void h() {}       // (1)
   protected static int i;
   private int j;
}
final class MyOtherClass extends MyClass
{
// MyOtherClass(int n) { m = n; }  //(2)
   public static void main(String[] args)
   {
       MyClass mc = new MyOtherClass();
   }
   void f() { }
   void h() { }
// void k() { i++; }      // (3)
// void l() { j++; }       // (4)
   int m;
}
(a)
(1)
(b)
(2)
(c)
(3)
(d)
(4)
(e)
None of the above.
102.
What would be the result of attempting to compile and run the following program?
       class MyClass
{
   static MyClass ref;
   String[] arguments;
   public static void main(String[] args)
   {
       ref = new MyClass();
       ref.func(args);
   }
   public void func(String[] args)
   {
       ref.arguments = args;
   }
}
(a)
The program will fail to compile, since the static method main() cannot have a call to the non-static method func()
(b)
The program will fail to compile, since the non-static method func() cannot access the static variable ref
(c)
The program will fail to compile, since the argument args passed to the static method main() cannot be passed on to the non-static method func()
(d)
The program will compile, but will throw an exception when run
(e)
The program will compile and run successfully.
103.
Given the following member declarations, which statement is true?
int a;                                                     //(1)
static int a;                                                        //(2)
int f() { return a; }                                    //(3)
static int f() { return a; }   //(4)
(a)
Declarations (1) and (3) cannot occur in the same class definition
(b)
Declarations (2) and (4) cannot occur in the same class definition
(c)
Declarations (1) and (4) cannot occur in the same class definition
(d)
Declarations (2) and (3) cannot occur in the same class definition
(e)
None of the above.
104.
Which of these combinations of switch expression types and case label value types are legal within a switch statement?
(a)
switch expression of type int and case label value of type char
(b)
switch expression of type float and case label value of type int
(c)
switch expression of type byte and case label value of type float
(d)
switch expression of type char and case label value of type long
(e)
switch expression of type boolean and case label value of type boolean.
105.
What, if anything, is wrong with following code?
 void test(int x)
   {
   switch (x)
   {
   case 1:
   case 2:
   case 0:
   default:
   case 4:
   }
}
(a)
The variable x does not have the right type for a switch expression
(b)
The case label 0 must precede case label 1
(c)
Each case section must end with a break statement
(d)
The default label must be the last label in the switch statement
(e)
There is nothing wrong with the code.
106.
What will be the result of attempting to compile and run the following code?
class MyClass
{
   public static void main(String[] args)
   {
          boolean b = false;
          int i = 1;
          do
          {
                 i++;
                 b = !b;
          } while (b);
          System.out.println(i);
   }
}
(a)
The code will fail to compile, since b is an invalid conditional expression in the
do-while statement
(b)
The code will fail to compile, since the assignment b = !b is not allowed
(c)
The code will compile without error and will print 1 when run
(d)
The code will compile without error and will print 2 when run
(e)
The code will compile without error and will print 3 when run.
107.
Given the following code, which statement is true?
         class MyClass
{
   public static void main(String[] args)
   {
          int k = 0;
          int l = 0;
          for (int i=0;i<=3;i++)
          {
                 k++;
                 if (i==2) break;
                 l++;
          }
          System.out.println(k+", "+l);
   }
}
(a)
The program will fail to compile
(b)
The program will print 3, 3 when run
(c)
The program will print 4, 3 when run if break is replaced by continue
(d)
The program will fail to compile if break is replaced by return
(e)
The program will fail to compile if break is simply removed.
108.
Which of the following is true?
(a)
If an exception is uncaught in a method, the method will terminate and normal execution will resume
(b)
An overriding method must declare that it throws the same exception classes as the method it overrides
(c)
The main() method of a program can declare that it throws checked exceptions
(d)
finally blocks are executed if, and only if, an exception gets throws while inside the corresponding try block
(e)
None of the above.
109.
What will be the result of attempting to compile and run the following program?
public class MyClass
{
   public static void main(String[] args)
   {
          RuntimeException re = null;
          throw re;
   }
}
(a)
The code will fail to compile, since the main() method does not declare that it throws Runtime Exception in its declaration
(b)
The program will fail to compile, since it cannot throw re
(c)
The program will compile without error and will throw java.lang.RuntimeException when run
(d)
The program will compile without error and will throw java.lang.NullpointerException when run
(e)
The program will compile without error and will run and terminate without any output.
110.
Which of the following statements is true?
(a)
The subclass of a non-abstract class can be declared abstract
(b)
All the members of the superclass are inherited by the subclass
(c)
A final class can be abstract
(d)
A class in which all the members are declared private, cannot be declared public
(e)
In Java the extends clause is used to specify interface.





Answers




101.
Answer : (c)
Reason:  The line void k() { i++; } can be re-inserted without introducing errors. Re-inserting the line (1) will cause the compilation to fail, since MyOtherClass will try to override a final method. Re-inserting line (2) will fail, since MyOtherClass will no longer have a default constructor. The main() method needs to call the default constructor. Re-inserting line (3) will work without any problems, but re-inserting line (4) will fail, since the method will try to access a private member of the superclass.
102.
Answer : (e)
Reason:  An object reference is needed to access non-static members. Static methods do not have the implicit object reference this, and must always supply an explicit object reference when referring to non-static members. The static method main() refers legally to the non-static method func() using the reference variable ref. Static members are accessible both from static and non-static methods, using their simple names.
103.
Answer : (c)
Reason:  Local variables can have the same name as member variables. The local variables will simply shadow the member variables with the same names. Declaration (4) defines a static method that tries to access a variable named a, which is not locally declared. Since the method is static, this access will only be valid if variable a is declared static within the class. Therefore, declarations (1) and (4) cannot occur in the same class definition, while declarations (2) and (4) can.
104.
Answer : (a)
Reason:  The type of the switch expression must be byte, char, short, or int. This excludes (b) and (e). The type of the case labels must be assignable to the type of the switch expression. This excludes (c) and (d).
105.
Answer : (e)
Reason:  There is nothing wrong with the code. The case and default labels do not have to be specified in any specific order. The use of the break statement is not mandatory, and without it the control flow will simply fall through the labels of the switch statement.
106.
Answer : (e)
Reason:  The loop body is executed twice and the program will print 3. The first time the loop is executed, the variable i changes from 1 to 2 and the variable b changes from false to true. Then the loop condition is evaluated. Since b is true, the loop body is executed again. This time the variable i changes from 2 to 3 and the variable b changes from true to false. The loop condition is now evaluated again. Since b is now false, the loop terminates and the current value of i is printed.
107.
Answer : (c)
Reason:  As it stands, the program will compile correctly and will print “3, 2” when run. If the break statement is replaced with a continue statement, the loop will perform all four iterations and will print “4, 3”. If the break statement is replaced with a return statement, the whole method will end when i equals 2, before anything is printed. If the break statement is simply removed, leaving the empty statement (;), the loop will complete all four iterations and will print “4, 4”.
108.
Answer : (c)
Reason:  Normal execution will only resume if the exception is caught by the method. The uncaught exception will propagate up the runtime stack until some method handles it. An overriding method need only declare that it can throw a subset of the checked exceptions the overridden method can throw. The main() method can declare that it throws checked exceptions just like any other method. The finally block will always be executed, no matter how control leaves the try block.
109.
Answer : (d)
Reason:  The program will compile without error, but will throw a NullPointerException when run. The throw statement can only throw Throwable objects. A NullPointerException will be thrown if the expression of the throw statement results in a null reference.
110.
Answer : (a)
Reason:  A subclass can be declared abstract regardless of whether the superclass was declared abstract. Private, overridden, and hidden members from the superclass are not inherited by the subclass. A class cannot be declared both abstract and final, since an abstract class needs to be extended to be useful and a final class cannot be extended. The accessibility of the class is not limited by the accessibility of its members. A class with all the members declared private can still be declared public. The extends clause is used to specify that a class extends another class, namely inheritance.



<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


5 comments :

  1. Can you tell m what is the responsibility of encapsulation in java ? Is it security or diversity ?

    ReplyDelete
  2. Thanks for sharing the post. . parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family.

    http://www.java-tips.org/java-tutorials.html

    ReplyDelete