Sie sind auf Seite 1von 12

SECTION A 11.

Given the following piece of code:


class SalaryCalculationException extends Exception{}
1.The class at the top of exception class hierarchy is ................. class Person{
A. ArithmeticException B. Throwable public void calculateSalary() throws
C. Object D. Exception SalaryCalculationException{
2.In which of the following package Exception class exist? //...
A. java.util B. java.file throw new SalaryCalculationException();
C. java.io D. java.lang //...
E. java.net }
3.Exception generated in try block is caught in ........... block. }
A. catch B. throw class Company{
C. throws D. finally public void paySalaries(){
4.Which keyword is used to explicitly throw an exception? new Person().calculateSalary();
A. try B. throwing }
C. catch D. throw }
5.Which exception is thrown when divide by zero statement Which of the following statements is correct?
executes? 1. This code will compile without any problems.
A. NumberFormatException B. ArithmeticException 2. This code will compile if in method paySalaries() we return a
C. NullPointerException D. None of these boolean in stead of void.
6.Which keyword is used to specify the exception thrown by 3. This code will compile if we add a try-catch block in
method? paySalaries().
A. catch B. throws 4. This code will compile if we add throws
C. finally D. throw SalaryCalculationException in the signature of method
7.Which of the following blocks execute compulsorily whether paySalaries().
exception is caught or not. A. 1 and 4 B. 2 and 3
A. finally B. catch C. 2 and 4 D. 3 and 4
C. throws D. throw E. 1 and 2
8.What happen in case of multiple catch blocks? 12.What will be the output of the following piece of code:
A. Either super or subclass can be caught first. class Person{
B. The superclass exception must be caught first. public void talk() {}
C. The superclass exception cannot caught first. }
D. None of these public class Test{
9.Which exception is thrown when an array element is accessed public static void main(String args[]){
beyond the array size? Person p = null;
A. ArrayElementOutOfBounds try{
B. ArrayIndexOutOfBoundsException p.talk();
C. ArrayIndexOutOfBounds }
D. None of these catch(NullPointerException e){
10.What is the output of the following program code? System.out.print("There is a NullPointerException. ");
public class Test{ }
public static void main(String args[]){ catch(Exception e){
try{ System.out.print("There is an Exception. ");
int i; }
return; System.out.print("Everything went fine. ");
} }
catch(Exception e){ }
System.out.print("inCatchBlock"); A. There is a NullPointerException. Everything went fine.
} B. There is a NullPointerException.
finally{ C. There is a NullPointerException. There is an Exception.
System.out.println("inFinallyBlock"); D. This code will not compile, because in Java there are no pointers.
} 13.Determine output of the following program code?
} public class Test{
} public static void main(String args[]){
A. inCatchBlock int i;
B. inCatchBlock inFinallyBlock try{
C. inFinallyBlock i = calculate();
D. The program will return without printing anything System.out.println(i);
}catch(Exception e){
System.out.println("Error occured"); }
} }
} A. Overridden: hello (String, Integer[])
static int calculate(){ B. hello (String, Integer[])
return (7/2); C. Compilation fails
} D. None of these
} 17.
A. 3 B. 3.5 try{
C. Error occured D. Compilation Error File f = new File("a.txt");
E. None of these }catch(Exception e){
14.public class Test{ }catch(IOException io){
public static void main(String args[]){ }
try{ Is this code create new file name a.txt ?
int a = Integer.parseInt("four"); A. true B. false
} C. Compilation Error D. None of these
} 18.What is the output for the below code ?
} import java.io.FileNotFoundException;
Which exception could be handled by the catch block for above? class A{
A. IllegalStateException B. NumberFormatException public void printName() throws FileNotFoundException{
C. ClassCastException D. ArrayIndexOutOfBoundsException System.out.println("Value-A");
E. None of these }
15.What will be the output? }
class MyClass{ class B extends A{
public String test(){ public void printName() throws NullPointerException{
try{ System.out.println("Name-B");
System.out.print("One"); }
return ""; }
} public class Test{
finally{ public static void main (String[] args) throws Exception{
System.out.print("Two"); A a = new B();
} a.printName();
} }
} }
public class Test{ A. Value-A
public static void main(String args[]){ B. Compilation fails-Exception NullPointerException is not
MyClass m = new MyClass(); compatible with throws clause in A.printName()
m.test(); C. Name-B
} D. Compilation succeed but no output
} E. None of these
A. One B. Two C. One Two 19.What will be the result of executing the following code?
D. Compilation Error E. None of these public class Test{
16.What will be the result after compiling this code? public void divide(int a, int b){
class SuperClass{ try{
public int doIt(String str, Integer... data)throws Exception{ int c = a / b;
String signature = "(String, Integer[])"; }catch(Exception e){
System.out.println(str + " " + signature); System.out.print("Exception ");
return 1; }finally{
} System.out.println("Finally");
} }
public class Test extends SuperClass{ public static void main(String args[]){
public int doIt(String str, Integer... data){ Test t = new Test();
String signature = "(String, Integer[])"; t.divide(0,3);
System.out.println("Overridden: " + str + " " +signature); }
return 0; }
} A. Prints out: Exception B. Prints out: Exception Finally
public static void main(String... args){ C. Compile with error D. Prints out: Finally
SuperClass sb = new Test(); E. None of these
sb.doIt("hello", 3);
20.Which of the below statement is/are true about Error? A. 1 B. 3 C. 2 3 D. 1 3 E. 1 2 3
A. An Error is a subclass of Throwable. 24.What will be the result after the class Test execution?
B. An Error is a subclass of Exception. class A{
C. Error indicates serious problems that a reasonable application public void doA(){
should not try to catch. B b = new B();
D. An Error is a subclass of IOException. b.dobB();
A. A and D B. A and B System.out.print("doA");
C. A and C D. B and C }
E. B and D }
21.Predict the output: class B{
public class Test{ public void dobB(){
public static void main(String args[]){ C c = new C();
try{ c.doC();
String arr[] = new String[10]; System.out.print("doB");
arr = null; }
arr[0] = "one"; }
System.out.print(arr[0]); class C{
}catch(Exception ex){ public void doC(){
System.out.print("exception"); if(true)
}catch(NullPointerException nex){ throw new NullPointerException();
System.out.print("null pointer exception"); System.out.print("doC");
} }
} }
} public class Test{
A. "one" is printed. public static void main(String args[]){
B. "exception" is printed. try{
C. "null pointer exception" is printed. A a = new A();
D. Compilation fails saying NullPointerException has already been a.doA();
caught. }catch(Exception ex){
E. None of these System.out.print("error");
22.Given the code. What is the result when this program is }
executed? }
public class Test{ }
static int x[]; A. "doCdoBdoA" is printed B. "doAdoBdoC" is printed
static{ C. "doBdoAerror" is printed D. "error" is printed
x[0] = 1; E. nothing is printed
}
public static void main(String args[]){ SECTION B
}
} 1.Which is true?
A. ArrayIndexOutOfBoundsException is thrown A. "X extends Y" is correct if and only if X is a class and Y is an
B. ExceptionInInitializerError is thrown interface
C. IllegalStateException is thrown B. "X extends Y" is correct if and only if X is an interface and Y is a
D. StackOverflowException is thrown class
E. None of these C. "X extends Y" is correct if X and Y are either both classes or both
23.What will be the result if NullPointerException occurs at line 2? interfaces
1. try{ D. "X extends Y" is correct for all combinations of X and Y being
2. //some code goes here classes and/or interfaces
3. } 2. Which of the following is true?
4. catch(NullPointerException ne){ 1. A class can extend more than one class.
5. System.out.print("1 "); 2. A class can extend only one class but many interfaces.
6. } 3. An interface can extend many interfaces.
7. catch(RuntimeException re){ 4. An interface can implement many interfaces.
8. System.out.print("2 "); 5. A class can extend one class and implement many interfaces.
9. } A. 1 and 2 B. 2 and 4 C. 3 and 5 D. 3 and 4
10. finally{ E. 2 and 5
11. System.out.print("3"); 3.What is the result of compiling and running the following code?
12. }
class Base{ A. Compilation Error B. Class One method1
public Base(){ C. Class Two method1 D. Throws a NoSuchMethodException at
System.out.print("Base"); runtime. E. None of these
}} 7.What is the result of compiling and running this program?
public class Derived extends Base{ class Mammal{
public Derived(){ void eat(Mammal m){
this("PSIT"); System.out.println("Mammal eats food");
System.out.print("Derived"); }}
} class Cattle extends Mammal{
public Derived(String s){ void eat(Cattle c){
System.out.print(s); System.out.println("Cattle eats hay");
} }}
public static void main(String[] args){ class Horse extends Cattle{
new Derived(); void eat(Horse h){
}} System.out.println("Horse eats hay");
A. PSITDerived B. PSITBaseDerived }}
C. BasePSITDerived D. PSITDerivedBase public class Test{
E. Compilation Error public static void main(String[] args){
4.What is the output of the following program code? Mammal h = new Horse();
abstract class C1{ Cattle c = new Horse();
public C1(){ c.eat(h);
System.out.print(1); }}
}} A. prints "Mammal eats food" B. prints "Cattle eats hay"
class C2 extends C1{ C. prints "Horse eats hay" D. Class cast Exception at runtime.
public C2(){ E. None of these
System.out.print(2); 8.Determine output:
}} class A{
class C3 extends C2{ public void method1(){
public C3(){ System.out.print("Class A method1");
System.out.println(3); }}
}} class B extends A{
public class Test{ public void method2(){
public static void main(String[] a){ System.out.print("Class B method2");
new C3(); }}
}} class C extends B{
A. 12 B. 23 C. 123 D. 321 public void method2(){
5.The concept of multiple inheritance is implemented in Java by System.out.print("Class C method2");}
I. Extending two or more classes. public void method3(){
II. Extending one class and implementing one or more interfaces. System.out.print("Class C method3");
III. Implementing two or more interfaces. }}
A. Only (II) B. (I) and (II) C. (II) and (III) D. Only (I) public class Test{
E. Only (III) public static void main(String args[]){
6.What will be the output? A a = new A();
interface A{ C c = new C();
public void method1();} c.method2();
class One implements A{ a = c;
public void method1(){ a.method3();
System.out.println("Class One method1"); }}
}} A. Class B method2 Class C method3
class Two extends One{ B. Class C method2 Class C method3
public void method1(){ C. Compilation Error D. Runtime exception
System.out.println("Class Two method1"); E. None of these
}} 9.What will be printed after executing following program code?
public class Test extends Two{ class Base{
public static void main(String[] args){ int value = 0;
A a = new Two(); Base(){
a.method1(); addValue();
}} }
void addValue(){ public void printValue(){
value += 10; System.out.print("Value-B");
} }}
int getValue(){ public class Test{
return value; public static void main(String args[]){
}} A a = new B();
class Derived extends Base{ a.printValue();
Derived(){ System.out.print(a.i);
addValue(); }}
} A. Value-B 11 B. Value-B 10
void addValue(){ C. Value-A 10 D. Value-A 11 E. None of these
value += 20; 13.What will be the result after compiling this code?
}} class SuperClass{
public class Test{ public int doIt(String str, Integer... data)throws Exception{
public static void main(String[] args){ String signature = "(String, Integer[])";
Base b = new Derived(); System.out.println(str + " " + signature);
System.out.println(b.getValue()); return 1;
}} }}
A. 30 B. 10 C. 40 D. 20 E. None of these public class Test extends SuperClass{
10.What will be the output? public int doIt(String str, Integer... data){
class Parent{ String signature = "(String, Integer[])";
public void method(){ System.out.println("Overridden: " + str + " " +signature);
System.out.println("Hi i am parent"); return 0;
}} }
public class Child extends Parent{ public static void main(String... args){
protected void method(){ SuperClass sb = new Test();
System.out.println("Hi i am Child"); sb.doIt("hello", 3);
} }}
public static void main(String args[]){ A. Overridden: hello (String, Integer[])
Child child = new Child(); B. hello (String, Integer[]) C. Compilation fails
child.method(); D. None of these
}} 14.
A. Compiles successfully and print class A{
B. Compiles successfully and print A(String s){}
C. Compile time error D. Run Time error E. None of This A(){}
11.What will be the output? }
class One{ 1. class B extends A{
final int a = 15; 2. B(){}
} 3. B(String s){
class Two extends One{ 4. super(s);
final int a = 20; 5. }
} 6. void test(){
public class Test extends Two{ 7. // insert code here
final int a = 30; 8. } 9. }
public static void main(String args[]){ Which of the below code can be insert at line 7 to make clean
Test t = new One(); compilation ?
System.out.print(t.a); A. A a = new B(); B. A a = new B(5);
}} C. A a = new A(String s); D. All of the above
A. 15 B. 20 C. 30 E. None of these
D. Compiler Error E. None of these 15.Determine output:
12.What will be the output? class A{
class A{ public void printValue(){
int i = 10; System.out.println("Value-A");
public void printValue(){ }}
System.out.print("Value-A"); class B extends A{
}} public void printNameB(){
class B extends A{ System.out.println("Name-B");
int i = 12; }}
class C extends A{ }}
public void printNameC(){ A. Value-A B. Name-B
System.out.println("Name-C"); C. Value-A Name-B
}} D. Compilation fails - private methods can't be override
E. None of these
1. public class Test{ 18.What will be the result of compiling and running the given
2. public static void main (String[] args){ code?
3. B b = new B(); class A{
4. C c = new C(); int b=10;
5. newPrint(b); private A(){
6. newPrint(c); this.b=7;
7. } }
8. public static void newPrint(A a){ int f(){
9. a.printValue(); return b;
10. } 11. } }}
A. Value-A Name-B B. Value-A Value-A class B extends A{
C. Value-A Name-C D. Name-B Name-C int b;}
E. None of these public class Test{
16. Determine output: public static void main(String[] args){
class A{ A a = new B();
public void printName(){ System.out.println(a.f());
System.out.println("Name-A"); }}
}} A. Compilation Fails B. Prints 0
class B extends A{ C. Prints 10 D. Prints 7 E. None of these
public void printName(){ 19.What will be the result of compiling and executing the following
System.out.println("Name-B"); program code?
}} class Vehicle{
class C extends A{ public void printSound(){
public void printName(){ System.out.print("vehicle");
System.out.println("Name-C"); }}
}} class Car extends Vehicle{
1. public class Test{ public void printSound(){
2. public static void main (String[] args){ System.out.print("car");
3. B b = new B(); }}
4. C c = new C(); class Bike extends Vehicle{
5. b = c; public void printSound(){
6. newPrint(b); System.out.print("bike");
7. } }}
8. public static void newPrint(A a){ public class Test{
9. a.printName(); public static void main(String[] args){
10. } 11. } Vehicle v = new Car();
A. Name B B. Name C Bike b = (Bike) v;
C. Compilation fails due to an error on lines 5 v.printSound();
D. Compilation fails due to an error on lines 9 b.printSound();
E. None of these }}
17.What is the output for the below code ? A. Compilation fails.
class A{ B. ClassCastException exception is thrown at runtime.
private void printName(){ C. "vehiclecar" is printed. D. "vehiclebike" is printed.
System.out.println("Value-A"); E. "carcar" is printed.
}} 20. Determine output:
class B extends A{ class Small{
public void printName(){ public Small(){
System.out.println("Name-B"); System.out.print("a ");
}} }}
public class Test{ class Small2 extends Small{
public static void main (String[] args){ public Small2(){
B b = new B(); System.out.print("b ");
b.printName(); }}
class Small3 extends Small2{ C. abstract interface A { abstract void
public Small3(){ D. interface A { void
System.out.print("c "); 7.Determine output of the following code.
}} interface A { }
public class Test{ class C { }
public static void main(String args[]){ class D extends C { }
new Small3(); class B extends D implements A { }
}} public class Test extends Thread{
A. a B. c C. a b c public static void main(String[] args){
D. c b a E. The code runs without output.. B b = new B();
if (b instanceof A)
SECTION C System.out.println("b is an instance of A");
if (b instanceof C)
1.Given the following piece of code: System.out.println("b is an instance of C");
public class School{ }
public abstract double numberOfStudent(); }
} A. Nothing. B. b is an instance of A.
which of the following statements is true? C. b is an instance of C.
A. The keywords public and abstract cannot be used together. D. b is an instance of A followed by b is an instance of C.
B. The method numberOfStudent() in class School must have a 8. Given the following piece of code:
body. public interface Guard{
C. You must add a return statement in method numberOfStudent(). void doYourJob();
D. Class School must be defined abstract. }
2. Which of the following class definitions defines a legal abstract abstract public class Dog implements Guard{ }
class? which of the following statements is correct?
A. class A { abstract void unfinished() { } } A. This code will not compile, because method doYourJob() in
B. class A { abstract void unfinished(); } interface Guard must be defined abstract.
C. abstract class A { abstract void unfinished(); } B. This code will not compile, because class Dog must implement
D. public class abstract A { abstract void unfinished(); } method doYourJob() from interface Guard.
3.Which of the following declares an abstract method in an C. This code will not compile, because in the declaration of class Dog
abstract Java class? we must use the keyword extends instead of implements.
A. public abstract method(); D. This code will compile without any errors.
B. public abstract void method(); 9. In Java, declaring a class abstract is useful
C. public void abstract Method(); A. To prevent developers from further extending the class.
D. public void method() {} B. When it doesn't make sense to have objects of that class.
E. public abstract void method() {} C. When default implementations of some methods are not
4.Which of the following statements regarding abstract classes are desirable.
true? D. To force developers to extend the class not to use its capabilities.
A. An abstract class can be extended. E. When it makes sense to have objects of that class.
B. A subclass of a non-abstract superclass can be abstract.
C. A subclass can override a concrete method in a superclass to 10.What will be the output?
declare it abstract. interface A{
D. An abstract class can be used as a data type. public void method();
E. All of the above }
5. Suppose A is an abstract class, B is a concrete subclass of A, and class One{
both A and B have a default constructor. Which of the following is public void method(){
correct? System.out.println("Class One method");
1. A a = new A(); }
2. A a = new B(); }
3. B b = new A(); class Two extends One implements A{
4. B b = new B(); public void method(){
A. 1 and 2 B. 2 and 4 System.out.println("Class Two method");
C. 3 and 4 D. 1 and 3 }
E. 2 and 3 }
6.Which of the following is a correct interface? public class Test extends Two{
A. interface A { void public static void main(String[] args){
B. abstract interface A { print(); A a = new Two();
a.method();
} }
} }
A. will print Class One method B. will print Class Two method A. Successful run and print 37
C. compiles fine but print nothing D. Compilation Error B. Compilation error due to line 1
E. None of these C. Compilation error due to line 2
11. D. Runtime error
interface Base{ E. None of these
boolean m1 (); 15. Runnable is a _____ .
byte m2(short s); A. class B. abstract class
} C. interface D. vaiable E. method
which two code fragments will compile? 16. What is the output for the below code ?
1. interface Base2 implements Base {} interface A{
public void printValue();
2. abstract class Class2 extends Base }
{ public boolean m1(){ return true; }} 1. public class Test{
2. public static void main (String[] args){
3. abstract class Class2 implements Base {} 3. A a1 = new A(){
4. public void printValue(){
4. abstract class Class2 implements Base 5. System.out.println("A");
{ public boolean m1(){ return (7 > 4); }} 6. }
7. };
5. abstract class Class2 implements Base 8. a1.printValue();
{ protected boolean m1(){ return (5 > 7) }} 9. }
A. 1 and 2 B. 2 and 3 C. 3 and 4 10. }
D. 1 and 3 E. 4 and 5 A. Compilation fails due to an error on line 3
12.Which two of the following are legal declarations for abstract B. A
classes and interfaces? C. Compilation fails due to an error on line 8
1. final abstract class Test {} D. null
2. public static interface Test {} E. None of these
3. final public class Test {} 17.What will be the output?
4. protected abstract class Test {} 1. public interface InfA{
5. protected interface Test {} 2. protected String getName();
6. abstract public class Test {} 3. }
A. 1 and 2 B. 2 and 4 C. 3 and 5 public class Test implements InfA{
D. 5 and 6 E. 3 and 6 public String getName(){
13. return "test-name";
interface Test{ }
int p = 10; //line 1 public static void main (String[] args){
public int q = 20; //line 2 Test t = new Test();
public static int r = 30; //line 3 System.out.println(t.getName());
public static final int s = 40; //line 4 }
} }
Which of the above line will give compilation error? A. test-name
A. 1 B. 2 C. 3 D. 4 E. None of these B. Compilation fails due to an error on lines 2
14.What will happen after compiling this program code? C. Compilation fails due to an error on lines 1
abstract class MyClass{ //line 1 D. Compilation succeed but Runtime Exception
private int a, b; E. None of these
18.What will be the output for the below code ?
public void call(int a, int b){ public interface TestInf{
this.a = a; int i =10;
this.b = b; }
System.out.print(a+b); public class Test{
} public static void main(String... args){
} TestInf.i=12;
public class Test{ System.out.println(TestInf.i);
public static void main(String args[]){ }
MyClass m = new MyClass(); //line 2 }
m.call(12,25); A. Compile with error B. 10
C. 12 D. Runtime Exception 1.What is the expected output?
E. None of these public class Profile {
19. What will be the output when the following program is private Profile(int w) { // line 1
compiled and executed? System.out.print(w);
abstract class TestAbstract{ }
String my_name; public static Profile() { // line 5
String myName(){ System.out.print (10);
my_name = "MPEC"; }
return my_name; public static void main(String args[]) {
} Profile obj = new Profile(50);
abstract void display(); }
} }
public class Test extends TestAbstract{ 2.The following code contains one compilation error, find it?
void display(){ public class Test {
String n = myName(); Test() { } // line 1
System.out.print("My name is "+ n); static void Test() { this(); } // line 2
} public static void main(String[] args) { // line 3
public static void main(String args[]){ Test(); // line 4
Test t = new Test(); }
t.display(); }
} 3.What will be the return type of a method that not returns any
} value?
A. Program will compile and execute successfully and prints A. void B. int C. double D. None of the above
B. Compilation error as class cannot be declared as abstract. 4.Which of the following options is the best for generating random
C. Program compiles but leads to runtime exception. integer 0 or 1?
D. Compilation error occurs as the abstract class TestAbstract A. (int)Math.random() B. (int)Math.random() + 1
contains a non-abstract method. C. (int)(Math.random() + 0.5) D. (int)(Math.random() + 0.2)
E. None of these 5. What is Math.floor(3.6)?
20. What happens if the following program is compiled and A. 3.0 B. 3 C. 4 D. 4.0
executed? 6. In which area of memory, the system stores parameters and
interface MyInterface{ local variables whenever a method is invoked?
void display(); A. Heap B. Storage Area C. Stack D. Array
} 7.What is the expected output?
interface MySubInterface extends MyInterface{ public class Profile {
void display(); private Profile(int w) { // line 1
} System.out.print(w);
public class Test implements MySubInterface{ }
public void display(){ public final Profile() { // line 5
System.out.print("Welcome to MPEC."); System.out.print(10);
} }
public static void main(String args[]){ public static void main(String args[]) {
Test t = new Test(); Profile obj = new Profile(50);
t.display(); }
} }
} 8.What is the expected output?
A. The code will lead to a compilation error as declaration of the class Animal {
display method has been provided in two interface. Animal() {
B. The code will lead to a compilation error due to public modifier System.out.println("Animal");
while declaring the display method. }
C. The code will compile and execute successfully showing the }
output Welcome to MPEC. class Wild extends Animal{
D. The code will lead to a compilation error as the display method is Wild() {
not declared as abstract. System.out.println("Wild");
E. None of these super();
}
SECTION D }
public class Test {
public static void main(String args[]) {
Wild wild = new Wild(); D. Before garbage collection.
} E. None of the above
} 17.The main method should be static for the reason
9.Which of the modifier can't be used for constructors? A. It can be accessed easily by the class loader.
A. public B. private C. static D. protected B. It can be accessed by every method or variable without any
10.The variables declared in a class for the use of all methods of hindrance.
the class are called C. It can be executed without creating any instance of the class.
A. reference variables B. objects D. None of the above
C. instance variables D. None of these 18.Given the following piece of code:
11.Determine output of the following program. class Person{
public class Test{ public int number;
public static void main(String args[]){ }
System.out.println( Math.floor( Math.random( ) ) ) ; public class Test{
} public void doIt(int i , Person p){
} i = 5;
12.What is the output of the above program ? p.number = 8;
class Num { }
Num(double x ){ public static void main(String args[]){
System.out.println( x ) ; int x = 0;
} Person p = new Person();
} new Test().doIt(x, p);
public class Test extends Num { System.out.println(x + " " + p.number);
public static void main(String[] args){ }
Num num = new Num( 2 ) ; }
} 19.Which of the following statements regarding static methods are
} correct?
13.Determine Output: 1. Static methods are difficult to maintain, because you can not
class A{ change their implementation.
public static void method(int i){ 2. Static methods can be called using an object reference to an
System.out.print("Method 1"); object of the class in which this method is defined.
} 3. Static methods are always public, because they are defined at
public static int method(String str){ class-level.
System.out.print("Method 2"); 4. Static methods do not have direct access to non-static methods
return 0; which are defined inside the same class.
} A. 1 and 2 B. 2 and 4 C. 3 and 4 D. 1 and 3
} 20.public class Test { }
public class Test{ What is the prototype of the default constructor?
public static void main(String args[]){ A. public Test(void) B. Test( )
A.method(5); C. Test(void) D. public Test( )
} E. None of these
} 21. Determine output:
14.What is the output of the program? class MyClass{
class Test{ MyClass(){
public int display(int x, int y){ System.out.print("one");
return ("The sum of x and y is " + x + y); }
} public void myMethod(){
public static void main(String args[]){ this();
Test test = new Test(); System.out.print("two");
System.out.println(test.display(4,5)); }
} }
} public class TestClass{
15.The implicit return type of a constructor is public static void main(String args[]){
A. void B. A class object in which it is defined. MyClass obj = new MyClass();
C. There is no return type. D. None of the above obj.myMethod();
16.The finalize() method is called just prior to }
A. An object, variable or method goes out of scope. }
B. An object or variable goes out of scope. 22.Determine output:
C. A variable goes out of scope. public class Test{
public static void main(String args[]){ }
MyClass obj = new MyClass(); }
obj.val = 1; 27.What is the output for the below code?
obj.call(obj); public class Test{
System.out.println(obj.val); public static void printValue(int i, int j, int k){
} System.out.println("int");
} }
class MyClass{ public static void printValue(byte...b){
public int val; System.out.println("long");
public void call(MyClass ref){ }
ref.val++; public static void main(String... args){
} byte b = 9;
} printValue(b,b,b);
23. Determine output: }
class MyClass{ }
int i; 28.class A{
int j; A(String s){}
public MyClass(int i, int j){ A(){}
this.i = i; }
this.j = j; 1. class B extends A{
} 2. B(){}
public void call(){ 3. B(String s){
System.out.print("One"); 4. super(s);
} 5. }
} 6. void test(){
public class Test{ 7. // insert code here
public static void main(String args[]){ 8. }
MyClass m = new MyClass(); //line 1 9. }
m.call(); //line 2 Which of the below code can be insert at line 7 to make clean
} compilation ?
} A. A a = new B(); B. A a = new B(5);
24.public class MyClass{ } C. A a = new A(String s); D. All of the above
For the above class(MyClass) what is the correct way of declaring E. None of these
constructor? 29.What is the output for the below code ?
A. MyClass(){} B. MyClass(void) {} class A{
C. public MyClass(){} D. public MyClass(void){} public A(){
E. 1 and 3 System.out.println("A");
25.What is the output for the below code ? }
1. public class A{ public A(int i){
2. int add(int i, int j){ this();
3. return i+j; System.out.println(i);
4. } }
5. } }
6. public class B extends A{ class B extends A{
7. public static void main(String argv[]){ public B(){
8. short s = 9; System.out.println("B");
9. System.out.println(add(s,6)); }
10. } public B(int i){
11.} this();
26.What will be the output? System.out.println(i+3);
public class Test{ }
public static void main(String[] args){ }
String value = "abc"; public class Test{
changeValue(value); public static void main (String[] args){
System.out.println(value); new B(5);
} }
public static void changeValue(String a){ }
a = "xyz";
30.Which of these is a legal definition of a method
named psit() assuming it throws IOException, and returns void. Also
assume that the method does not take any arguments. Select the
one correct answer.
A. void psit () {} throws IOException
B. void psit () throws IOException{}
C. void psit () throw IOException{}
D. void psit (void) throws IOException{}
E. psit () throws IOException{}
31. 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());
}
}

Das könnte Ihnen auch gefallen