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;
}
|
||||||||||
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;
}
}
|
||||||||||
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)
|
||||||||||
104.
|
Which of
these combinations of switch expression types and case label value types are
legal within a switch statement?
|
||||||||||
105.
|
What, if anything,
is wrong with following code?
void
test(int x)
{
switch (x)
{
case 1:
case 2:
case 0:
default:
case 4:
}
}
|
||||||||||
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);
}
}
|
||||||||||
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);
}
}
|
||||||||||
108.
|
Which of the
following is true?
|
||||||||||
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;
}
}
|
||||||||||
110.
|
Which of the
following statements is true?
|
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 >>
Can you tell m what is the responsibility of encapsulation in java ? Is it security or diversity ?
ReplyDeleteThanks 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.
ReplyDeletehttp://www.java-tips.org/java-tutorials.html
Very helpful
ReplyDeletethank you.
ReplyDeletejava tutorial
java tutorial
Lovely
ReplyDelete