Sie sind auf Seite 1von 17

Institute of Java & Software Engineering

Assignment No 02
Object Oriented Programming
Abstract Classes Interfaces Packages - PartIII

Question 1.
abstract class MyClass{ //insert code here //Line 10 } Which of the following code fragment could be inserted at line 10 and still allow the code to compile? a. final abstract void foo(); b. abstract void foo(); c. static abstract void foo(); d. private abstract void foo(); e. protected abstract void foo(); f. abstract void foo(); g. abstract void foo(){} h. void foo();

Question 5.
Which of the follow are true statements. a. An incompletely implemented class must be declared abstract. b. A class can be declared abstract without having any abstract method it self. c. An abstract class can be instantiated. d. An abstract class is implicitly final. e. An abstract class must declare at least one abstract method. f. An abstract class can not extend a concrete class.

Question 6.
class Rectangle{ double length; double width; Rectangle(){} Rectangle(double length, double width){ this.length=length; this.width=width; } double area(){return length*width;} } Which of the following code fragments are legal? a. abstract class Polygon extends Rectangle{} b. class Polygon extends Rectangle{} abstract class Polygon extends Rectangle{ abstract double volume(); } c. abstract class Polygon extends Rectangle{ abstract double area(); } d. abstract class Polygon extends Rectangle{ abstract double area(); abstract double area(double d); }

Question 2.
abstract class MyClass{ int value; //Line 1 int seek=200; //Line 2 MyClass(int i){}//Line 3 abstract MyClass(); //Line 4 abstract void method1(); void method2(); //Line 5 } Compile-time errors are generated at which lines? a. Line 1 b. Line 2 c. Line 3 d. Line 4 e. Line 5 f. None of the above

Question 3.
abstract class Shape{ abstract double area(); abstract double perimeter(); } Which of the following code fragments are legal? a. abstract class Rectangle extends Shape{} b. class Rectangle extends Shape{ double area(){return 1.0;} } c. class Rectangle extends Shape{ double area(){return 1.0;} double perimeter(){return 1.0;} } d. class Rectangle extends Shape{ abs tract double area(); double perimeter(){return 1.0;} } e. abstract class Rectangle extends Shape{ abstract double area(); abstract double perimeter(); }

Question 2.
abstract class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); } } class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){ System.out.println("My func"); } public void amethod(){ myfunc(); } } What will happen when you attempt to compile and run this code? a. The code will compile and run, printing out the words "MyFunc" b. The compiler will complain that the Base class has non abstract methods c. The code will compile but complain at run time that the Base class has non abstract methods d. The compiler will complain that the method myfunc in the base class has no body, nobody at all to loose it

Question 4.
Which of the following statements are true? a. An abstract class can never be instantiated. b. An abstract classes can contain both abstract and nonabstract methods c. If even a single method is marked abstract, the class must be marked abstract. d. All interface methods are implicitly public and abstract. e. All methods defined in an abstract class are implicitly abstract.

Institute of Java & Software Engineering

Question 3.
class Base{ abstract public void m(); public void another(){ System.out.println("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void m(){ System.out.println("M"); } public void amethod(){ m(); } } What will happen when you attempt to compile and run this code? a. The code will compile and run, printing out the words "M" b. The compiler will complain that the Base class is not declared as abstract c. The code will compile but complain at run time that the Base class has non abstract methods d. The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it

Question 5.
Which of the following code fragments are legal and which are not? Explain? A. public class SportsTournament { abstract void finalgame() {} void kickoffgame() {...} } class WorldCup extends SportsTournament { void finalgame() {...} } B. abstract class Animal { public abstract void travel() {} public abstract void feed() {} } class Mammal extends Animal{ void travel() {...} } C. interface Animal { void travel() {} void feed() {} } class Mammal implements Animal { void travel() {} } D. abstract public class Animal { abstract void travel() {} abstract void feed() {} } class Mammal extends Animal{ void travel() {...} void feed() {...} }

Question 4.
What will happen when you attempt to compile and run the following code? class Base{ protected int i = 99; } public class Ab{ private int i=1; public static void main(String argv[]){ Ab a = new Ab(); a.hallow(); } abstract void hallow(){ System.out.println("Claines "+i); } } a. Compile time error b. Compilation and output of Claines 99 c. Compilation and output of Claines 1 d. Compilation and not output at runtime

Question 6.
What will happen when you attempt to compile and run this code? class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){ System.out.println("My func"); } public void amethod(){ myfunc(); } } a) The code will compile and run, printing out the words "My Func" b) The compiler will complain that the Base class is not declared as abstract. c) The code will compile but complain at run time that the Base class has non abstract methods d) The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it

Institute of Java & Software Engineering

Question 7.
class Leg{} class Fur{} abstract class Pet { public abstract void eat(); public abstract void sleep(); } class Dog extends Pet { Leg leftFront = new Leg(); Leg rightFront = new Leg(); Leg leftRear = new Leg(); Leg rightRear = new Leg(); Fur fur = new Fur(); public Fur shed() {return fur;} public void eat() {} public void sleep() {} } class Cat extends Dog { public void ignoreOwner() {} public void climbTree() {} } Which of the following are true statements? a. A Cat object inherits Fur and four Legs from the Dog super class. b. A Cat object is able to sleep and eat. c. A Cat object is able to climb a tree. d. The relationship between Dog and Pet is an example of an appropriate use of inheritance. e. The relationship between Cat and Dog is an example of an appropriate use of inheritance.

Question 9.
Which of the following statements are true? a.. If a class has any abstract methods it must be declared abstract itself. b. All methods in an abstract class must be declared as abstract c. When applied to a class, the final modifier means it cannot be sub-classed d. transient and volatile are Java modifiers

Question 10.
abstract class A { private int x = 4; private int y = 2; public int x() { return x; } public void x(int x) { this.x = x; } public int y() { return y; } public void y(int y) { this.y = y; } public abstract int math(); } class B { static A a1 = new A(2,1) { public A(int i1, int i2) { x(i1); y(i2); }; public int math() { return x()+y(); } }; public static void main(String[] args) { System.out.print(a1.math()); } } What is the result of attempting to compile and run the program? a. Prints: 8 b. Prints: 3122 c. Compile-time error. d. Run-time error.

Question 8.
For each of the following code fragments, pleae indicate which has an overriden vs. overloaded method and explain why. i. abstract class Shape { public Shape (); void draw(); } class Circle extends Shape { public Circle() { ...} void draw(double x,double y, double radius) {...} } b. abstract class Shape { public Shape (); void draw(); } class Circle extends Shape { public Circle() { ...} void draw() {...} } c. abstract class Mammal { public Mammal(); Mammal giveBirth(); } class Dog extends Mammal { public Dog () {...} Dog giveBirth() {...} } d. abstract class Mammal { public Mammal(); Mammal giveBirth(); } class Dog extends Mammal { public Dog () {...} Dog giveBirth(int no_of_pups) {...} }

Institute of Java & Software Engineering

Question 11.
What is the correct ordering for the import, class and package declarations when found in a single file? Select the most appropriate answer. a. package, import, class b. class, import, package c. import, package, class d. package, class, import

Question 15.
You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java ? //Base.java package Base; class Base{ protected void amethod(){ System.out.println("amethod"); }//End of amethod }//End of class base package Class1; //Class1.java public class Class1 extends Base{ public static void main(String argv[]){ Base b = new Base(); b.amethod(); }//End of main }//End of Class1 A. Compile Error: Methods in Base not found B. Compile Error: Unable to access protected method in base class C. Compilation followed by the output "amethod" D. Compile error: Superclass Class1.Base of class Class1.Class1 not found

Question 12.
Which of the following code fragment could be inserted at line 12 and still allow the code to compile? interface Shape{ //Insert code here Line 12 } a. double area(); b. abstract double area(); c. abstract protected area(); d. private double area(); e. static double area(); f. private static double area(); g. private final double area(); h. public final double area(); i. public abstract double area();

Question 13.
//File P1.java package MyPackage; class P1{ void afancymethod(){ System.out.println("What a fancy method"); } } //File P2.java public class P2 extends P1{ afancymethod(); } What happens when you attempt to compile and run these two files in the same directory? a. Both compile and P2 outputs "What a fancy method" when run b. Neither will compile c. Both compile but P2 has an error at run time d. P1 compiles cleanly but P2 has an error at compile time

Question 16.
Which of the following will compile without error? a. import java.awt.*; package Mypackage; class Myclass {} b. package MyPackage; import java.awt.*; class MyClass{} /*This is a comment */ package MyPackage; import java.awt.*; class MyClass{}

c.

Question 14.
package p1; public class A{ protected void m(){ System.out.println("A.m()"); } } package p2; public class B extends p1.A{ int a; public static void main(String args[]){ p1.A r=new p1.A(); // 1 r.m(); // 2 } } What is the result of attempting to compile and run the program? a. A.m() b. null c. Compile time error at 1 d. Compile time error at 2 e. Run time error f. None of the above

Question 17.
package p1; public class A{ protected void m(){ System.out.println("A.m()"); } } package p2; public class B extends p1.A{ public void m(){ // 1 System.out.print("B.m()"); } public static void main(String args[]){ p1.A r=new B(); // 2 r.m(); // 3 } } What is the result of attempting to compile and run the program? a. A.m b. B.m c. A.mB.m d. Compile time error at 1 e. Compile time error at 2 f. Compile time error at 3 g. None of the above

Institute of Java & Software Engineering

Question 18.
package p1; public class A{ protected void m(){ System.out.println("A.m()"); } public static void main(String args[]){ A ob1=new p2.B(); ob1.m(); } } package p2; public class B extends p1.A{ public void m(){ System.out.print("B.m()"); } } What is the result of attempting to compile and run the program? a. A.m() b. B.m() c. Compile time error at 1 d. Compile time error at 2 f. None of the above

What is the result of attempting to compile and run the program? a. 0 b. 10 c. Compile time error at 1 d. Compile time error at 2 e. None of the above

Question 21.
Given the following three source files: 1. package org; 2. public class Robot { } 1. package org.ex; 2. public class Pet { } 1. package org.ex.why; 2. public class Dog { int foo = 5; } And the following incomplete source file: // insert code here public class MyClass { Robot r; Pet p; Dog d; void go() { int x = d.foo; } } Which statement(s) must be added for MyClass to compile? (Choose all that apply.) a. package org; b. import org.*; c. package org.*; d. package org.ex; e. import org.ex.*; f. import org.ex.why; g. package org.ex.why; f. package org.ex.why.Dog;

Question 19.
package p1; public class A{ protected void m(){ System.out.println("A.m()"); } } package p2; public class B extends p1.A{ public void m(){ System.out.print("B.m()"); super.m(); // 1 } public static void main(String args[]){ B ob1=new B(); ob1.m(); // 2 } } What is the result of attempting to compile and run the program? a. A.m() b. B.m() c. Compile time error at 1 d. Compile time error at 2 e. None of the above f. B.m()A.m() g. A.m()B.m()

Question 22.
Which of the following are correct? Select all correct answers? a. An import statement, if defined, must always be the first non-comment statement of the file. b. private members are accessible to all classes in the same package. c. An abstract class can be declared as final. d. Local variables cannot be declared as static.

Question 23. Given:


11. public interface Status { 12. /* insert code here */ int MY_VALUE = 10; 13. } Which three are valid on line 12? (Choose three.) a. final b. static c. native d. public e. private f. abstract g. protected

Question 20.
package p1; public class Test1{ protected int x=10; protected void m(){ System.out.println("A.m()"); } } package p2; import p1.*; public class Test2 extends Test1{ public void m(){ // 1 System.out.print(x); } public static void main(String args[]){ Test2 ob1=new Test2(); ob1.m(); // 2 }}

Question 24.
Which Man class properly represents the relationship Man has a best friend who is a Dog? a. class Man extends Dog { } b. class Man implements Dog { } c. class Man { private BestFriend dog; } d. class Man { private Dog bestFriend; } e. class Man { private Dog<bestFriend> } f. class Man { private BestFriend<dog> }

Institute of Java & Software Engineering

Question 25.
10. package com.sun.scjp; 11. public class Geodetics { 12. public static final double DIAMETER = 12756.32; //kilometers 13. } Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.) a. import com.sun.scjp.Geodetics; public class TerraCarta { public double halfway(){ return Geodetics.DIAMETER/2.0; } } b. import static com.sun.scjp.Geodetics; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } } c. import static com.sun.scjp.Geodetics. *; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } } d. package com.sun.scjp; public class TerraCarta { public double halfway() { return DIAMETER/2.0; } }

Question 28.
Given: 11. public abstract class Shape { 12. int x; 13. int y; 14. public abstract void draw(); 15. public void setAnchor(int x, int y) { 16. this.x = x; 17. this.y = y; 18. } 19. } and a class Circle that extends and fully implements the Shape class. Which is correct? A. Shape s = new Shape(); s.setAnchor(10,10); s.draw(); B. Circle c = new Shape(); c.setAnchor(10,10); c.draw(); C. Shape s = new Circle(); s.setAnchor(10,10); s.draw(); D. Shape s = new Circle(); setAnchor(10,10); draw(); E. Circle c = new Circle(); c.Shape.setAnchor(10,10); c.Shape.draw();

Question 29.
abstract public class Employee { protected abstract double getSalesAmount(); public double getCommision() { return getSalesAmount() * 0.15; } } class Sales extends Employee { // insert method here //Line 10 } Which two methods, inserted independently at line 10, correctly complete the Sales class? a. double getSalesAmount() { return 1230.45; } b. public double getSalesAmount() { return 1230.45; } c. private double getSalesAmount() { return 1230.45; } d. protected double getSalesAmount() { return 1230.45; }

Question 26.
Given: 10. interface Foo { int bar(); } 11. public class Sprite { 12. public int fubar( Foo foo) { return foo.bar(); } 13. public void testFoo() { 14. fubar( 15. // insert code here 16. ); 17. } 18. } Which code, inserted at line 15, allows the class Sprite to compile? a. Foo { public int bar() { return 1; } } b. new Foo { public int bar() { return 1; } } c. newFoo() { public int bar(){return 1; } } d. new class Foo { public int bar() { return 1; } }

Question 30.
A programmer is designing a class to encapsulate the information about an inventory item. A JavaBeans component is needed to do this. The Inventoryltem class has private instance variables to store the item information: private int itemId; private String name; private String description; Which method signature follows the JavaBeans naming standards for modifying the itemld instance variable? A. itemID(int itemId) B. update(int itemId) C. setItemId(int itemId) D. mutateItemId(int itemId) E. updateItemID(int itemId)

Question 27.
Given: 1. public interface A { 2. String DEFAULT_GREETING = Hello World; 3. public void method1(); 4. } A programmer wants to create an interface called B that has A as its parent. Which interface declaration is correct? a. public interface B extends A { } b. public interface B implements A {} c. public interface B instanceOf A {} d. public interface B inheritsFrom A { }

Institute of Java & Software Engineering

Question 31.
public abstract class Shape { private int x; private int y; public abstract void draw(); public void setAnchor(int x, int y) { this.x = x; this.y = y; } } Which two classes use the Shape class correctly? A. public class Circle implements Shape { private int radius; } B. public abstract class Circle extends Shape { private int radius; } C. public class Circle extends Shape { private int radius; public void draw(); } D. public abstract class Circle implements Shape { private int radius; public void draw(); } E. public class Circle extends Shape { private int radius; public void draw() {/* code here */} } F. public abstract class Circle implements Shape { private int radius; public void draw() { / code here */ } }

Question 33.
A JavaBeans component has the following field: private boolean enabled; Which two pairs of method declarations follow the JavaBeans standard for accessing this field? A. public void setEnabled( boolean enabled) public boolean getEnabled() B. public void setEnabled( boolean enabled) public void isEnabled() C. public void setEnabled( boolean enabled) public boolean isEnabled() D. public boolean setEnabled( boolean enabled) public boolean getEnabled()

Question 34.
1. package com.dan.chisholm; 2. 3. public class A { 4. public void m1() { System.out.print("A.m1, "); } 5. protected void m2() { System.out.print("A.m2, "); } 6. private void m3() { System.out.print("A.m3, "); } 7. void m4() { System.out.print("A.m4, "); } 8. } 9. 10. class B { 11. public static void main(String[] args) { 12. A a = new A(); 13. a.m1(); 14. a.m2(); 15. a.m3(); 16. a.m4(); 17. } 18. } Assume that the above code appears in a single file named A.java. What is the result of attempting to compile and run the program? a. Prints A.m1, A.m2, A.m3, A.m3, A.m4, d. Compiler error at line 15. b. Compiler error at line 13. e. Compiler error at line 16. c. Compiler error at line 14. f. None of the Above

Question 32.
10. interface Data { public void load(); } 11. abstract class Info { public abstract void load(); } Which class correctly uses the Data interface and Info class? A. public class Employee extends Info implements Data { public void load() { /*do something*/ } } B. public class Employee implements Info extends Data { public void load() { /*do something*/ } } C. public class Employee extends Info implements Data { public void load() { /*do something */ } public void Info.load() { /*do something*/ } } D. public class Employee implements Info extends Data { public void Data.load() { /*d something */ } public void load() { /*do something */ } } E. public class Employee implements Info extends Data { public void load() { /*do something */ } public void Info.load(){ /*do something*/ } } F. public class Employee extends Info implements Data{ public void Data.load() { /*do something*/ } public void Info.load() { /*do something*/ } }

Institute of Java & Software Engineering

Question 35.
1. 2. 3. 4. public class Basics {} public class Basics2 {} public class Basics3 {} public class Basics4 {} //Line 1 //Line 2 //Line 3 //Line 4

Question 40.
What will happen when you compile and run the following code? public class Scope{ private int i; public static void main(String argv[]){ Scope s = new Scope(); s.amethod(); }//End of main public static void amethod(){ System.out.println(i); }//end of amethod }//End of class a. A value of 0 will be printed out b. Nothing will be printed out c. A compile time error d. Compile time error complaining of the scope of the variable i

Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file? a. Compiler error at line 1. b. Compiler error at line 2 c. Compiler error at line 3. d. Compiler error at line 4 e. None of the Above

Question 36.
Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class. 1.public 2.protected 3.private 4.frendly 5.default

Question 41.
Which of the following is true? Select all correct answers. A. A class that is abstract may not be instantiated. B. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typicaly in C/C++. C. A static variable indicates there is only one copy of that variable. D. A method defined as private indicates that it is accessible to all other classes in the same package.

Question 37.
Which of the following are correct? Select all correct answers? A. An import statement, if defined, must always be the first non-comment statement of the file. B. private members are accessible to all classes in the same package. C. An abstract class can be declared as final. D. Local variables cannot be declared as static.

Question 42.
What will happen when you attempt to compile and run this code? class Base{ public final void amethod(){ System.out.println("amethod"); } } public class Fin extends Base{ public static void main(String argv[]){ Base b = new Base(); b.amethod(); } } A. Compile time error indicating that a class with any final methods must be declared final itself B. Compile time error indicating that you cannot inherit from a class with final method C. Run time error indicating that Base is not defined as final D. Success in compilation and output of "amethod" at run time.

Question 38.
Name the keyword which makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer. A. static B. final C. abstract D. native E. volatile F. transient

Question 39.
What will be the result when you try to compile and run the following code? private class Base{ Base(){ int i = 100; System.out.println(i); } } public class Pri extends Base{ static int i = 200; public static void main(String argv[]){ Pri p = new Pri(); System.out.println(i); } } A. Error at compile time B. 200 C. 100 followed by 200 D. 100

Question 43.
What will happen when you attempt to compile and run this code? public class Mod{ public static void main(String argv[]){ } public static native void amethod(); } a. Error at compilation: native method cannot be static b. Error at compilation native method must return value c. Compilation but error at run time unless you have made code containing native amethod available d. Compilation and execution without error

Institute of Java & Software Engineering

Question 44.
class A{ private int a=100; // 1 private static int b=200; public static void main(String args[]){ A ob=new A(); System.out.print(ob.a+" "); // 2 System.out.println(A.b); // 3 } } What is the result of attempting to compile and run the program? a. 100 200 b. 0 0 c. Compile time error at 1 f. Compile time error at 2 g. Compile time error at 3

Question 47.
class A{ private int a=100; void print(){ System.out.print(a); } } class B extends A{ private int a; void print(){ System.out.print(a); } public static void main(String args[]){ A a=new B(); a.print(); } } What is the result of attempting to compile and run the program? a. 100 b. 0 c. Compile time error at 1 d. Compile time error at 2 e. None of the above

Question 45.
class S{ private int x=100; private void m(){ // 1 System.out.print("S.m"); } } class C extends S{ private int y=80; private void m(){ // 2 System.out.print("C.m"); } } class DemoSC{ public static void main(String args[]){ S ob=new C(); ob.m(); } } What is the result of attempting to compile and run the program? a. S.m b. C.m c. S.mC.m d. Compile time error at 1 e. Compile time error at 2

Question 48.
Which of the following modifiers can be applied to a method? a. abstract b. final c. private d. protected e. public f. static g. synchronized h. transient i. volatile j. native k. strictfp l. None of the above.

Question 49.
Which of the following modifiers can be applied to a constructor? a. abstract b. final c. private d. protected e. public f. static g. synchronized h. transient i. volatile j. native k. strictfp l. None of the above.

Question 50.
Which of the following modifiers can be applied to a local class? a. public b. protected c. private d. abstract e. static f. final g. strictfp h. None of the above

Question 46.
Given the following code: public class Test { } Which of the following can be used to define a constructor for this class: Select the most appropriate answer A. public void Test() {} B. public Test() {} C. public static Test() {} D. public static void Test() {}

Question 51.
Which of the following modifiers can be applied to a field? a. abstract b. final c. private d. protected e. public f. static g. synchronized h. transient i. volatile j. native k. strictfp

Question 52.
Which of the following are class modifiers? Select all that are applicable to a top-level class and all that are applicable to a nested class. It is not required that a modifier be applicable to both. a. abstract b. extends c. final d. implements e. private f. protected g. public h. static i. synchronized j. transient k. volatile l. strictfp

Institute of Java & Software Engineering

Question 53.
class A { void m1() { public int a; // 1 protected int b; // 2 private int c; // 3 static int d; // 4 final int g=10; // 5 } } Compile-time errors are generated at which lines? a. Line 1 b. Line 2 c. Line 3 d. Line 4 e. Line 5

Question 57.
Given the following class definition public class Upton{ public static void main(String argv[]){ } public void amethod(int i){ } //Here } Which of the following would be legal to place after the comment //Here ? a. public int amethod(int z){} b. public int amethod(int i,int j){return 99;} c. protected void amethod(long l){ } d. private void anothermethod(){}

Question 54.
What will happen when you attempt to compile and run the following code? public class Sandys{ private int court; public static void main(String argv[]){ Sandys s = new Sandys(99); System.out.println(s.court); } Sandys(int ballcount){ court=ballcount; } } a. Compile time error, the variable court is defined as private b. Compile time error, s is not initialized when the System.out method is called c. Compilation and execution with no output d. Compilation and run with an output of 99

Question 58.
Which of the following statements are true? a. Constructors cannot have a visibility modifier b. Constructors can be marked public and protected, but not private c. Constructors can only have a primitive return type d. Constructors are not inherited

Question 59.
class Basics { private static int x = 1; protected static int y = 2; public static int z = 3; public static void main (String[] args) { System.out.println(x+y+z); } } What is the result of attempting to compile and run the program? a. Prints: 1 2 3 b. Prints: 123 c. Prints: 6 d. Compiler Error

Question 55.
class Basics { private static int x; protected static int y; public static int z; public static void main (String[] args) { System.out.println(x+y+z); } } What is the result of attempting to compile and run the program? a. Prints nothing. b. Prints an undefined value. c. Prints: null d. Prints: 0 e. Run time Exception f. Compiler Error

Question 60.
Which of the following statements are true? a. Static methods cannot be overriden to be non static b. Static methods cannot be overloaded c. Private methods cannot be overloaded d. An overloaded method cannot throw exceptions not checked in the base class

Question 61.
What will happen when you attempt to compile and run the following code? public class Hope{ public static void main(String argv[]){ Hope h = new Hope(); } protected Hope(){ for(int i =0; i <10; i ++){ System.out.println(i); } } } a. Compilation error: Constructors cannot be declared protected b. Run time error: Constructors cannot be declared protected c. Compilation and running with output 0 to 10 d. Compilation and running with output 0 to 9

Question 56.
class Basics { public static void main (String[] args) { private int x = 1; protected int y = 2; public int z = 3; System.out.println(x+y+z); } } What is the result of attempting to compile and run the program? a. Prints: 123 b. Prints: 1 2 3 c. Prints: 6 d. Run time error e. Compiler Error f. None of the Above

Institute of Java & Software Engineering

Question 62.
class Basics { private static int x=1; static void m(int i) { x++; i++; } public static void main (String[] args) { int y=3; Basics.m(y); System.out.println(x + "," + y); } } What is the result of attempting to compile and run the program? a. Prints: 1,3 b. Prints: 2,3 c. Prints: 1,3 d. Prints: 2,4 e. Compiler Error f. None of the Above

Question 66.
Which of the following code fragments are legal and which are not? Explain. 1. public final class FootballGame { void score() {...} } class AmericanFootballGame extends FootballGame{ void score() {...} } 2. public class FootballGame { final Ball b=new Ball("Adidas"); void test() { b.diameter=10; } } 3. public class FootballGame { final Ball b=new Ball("Adidas"); void kick() { b=new Ball("Nike"); } } 4. public class FootballGame { final void score() {...} } class AmericanFootballGame extends FootballGame { void score() {...} }

Question 63.
class Basics1 { // 1 public static void main(String[] args) { // 2 } // 3 } class Basics2 { // 4 protected static void main(String[] args) { // 5 } // 6 } class Basics3 { // 7 private static void main(String[] args) { // 8 } } What is the result of attempting to compile each of the three class declarations and invoke each main method from the command line . a. Compiler error at line 2. b. Compiler error at line 5. c. Compiler error at line 8 d. Run time error at line 2. e. Run time error at line 5. f. Run time error at line 8.

Question 67.
class Super{ private void m(){ System.out.print("super"); } } class Sub extends Super{ private void m(){ System.out.print("sub"); } } class Demo{ public static void main(String []args){ Super a=new Super(); a.m(); } } What is the result of attempting to compile and run the program? a. Prints: super b. Prints: sub c Runtime Exception. d. Compiler Error

Question 64.
1. public class Basics {} //Line 1 2. class Basics1 {} //Line 2 3. protected class Basics2 {} //Line 3 4. private class Basics3 {} //Line 4 5. Class Basics4 {} //Line 5 Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file? a. Compiler error at line 1. c. Compiler error at line 3. e. Compiler error at line 5. b. Compiler error at line 2. d. Compiler error at line 4. f. None of the Above

Question 68.
Which of the following statements are true? a. All of the variables in an interface are implicitly static b. All of the variables in an interface are implicitly final c. All of the methods in an interface are implicitly abstract d. A method in an interface can access class level variables

Question 65.
a. b. c. d. e. Which of the following statements are true? An interface can only contain method and not variables Interfaces cannot have constructors A class may extend only one other class and implement only one interface Interfaces are the Java approach to addressing its lack of multiple inheritance, but require mplementing classes to create the functionality of the Interfaces.

Institute of Java & Software Engineering

Question 69.
Given code: //--------------------A.java---------------------------package p1; public class A{ public A(){ myMethod(); } /*insert code here*/{ //Line 14 System.out.println("p1.A.myMethod"); } } //--------------------B.java---------------------------package p2; public class B extends p1.A{ public void myMethod(){ System.out.println("p2.B.myMethod"); } } //-------------------Demo.java-------------------------------package p3; public class Demo{ public static void main(String args[]){ new p2.B(); } } What will happen when you compile and run the above program with inserting followings at Line 14? A.void myMethod() B. protected void myMethod(); C.pubic void myMethod() D.private void myMethod() E.final void myMethod() Prints "p2.B.myMethod" prints "p2.B.myMethod" Prints "p2.B.myMethod" Prints "p1.A.myMethod" Compile Error, final method cannot overridden F.Insert public static void myMethod() prints "p1.A.myMethod"

Question 71.
//--------------------Car.java---------------------------package p1; public class Car{ //attributes protected void myMethod(){ System.out.println(any car); } } //--------------------Toyota.java---------------------------package p2; import p1.Car; public class Toyota extends Car{ void myMethod(){ Car c=new Car(); //Line 1 c.boost(); //Line 2 c.go(); //Line 3 Toyota t=new Toyota(); //Line 4 t.boost(); //Line 5 t.go(); //Line 6 boost(); //Line 7 go(); //Line 8 } } What will happen when you compile & run the program? A. Compile Error at Line 2 B. Compile Error at Line 3 C. Compile Error at Line 5 D. Compile Error at Line 6 E. Compile Error at Line 7 F. Compile Error at Line 8 G. Program will compile without errors

Question 72.
class Super{ private void m(){ System.out.print("super"); } } class Sub extends Super{ void m(){ System.out.print("sub"); } } class Demo{ public static void main(String []args){ Super a=new Sub(); a.m(); } } What is the result of attempting to compile and run the program? a. Prints: super b. Prints: sub c None of the Above e. Compiler Error

Question 70.
/----------A.java------------------------package p1; public class A{ //Insert code here Line 14 } //---------B.java------------------------package p2; public class B extends p1.A{ public B(){ } public B(int i){ } } Which methods, can be inserted independently at line 14, correctly compile the class B? A. A(){/*Code here*/} and A(int i){/*Code here*/} B. A(){/*Code here*/} C. Insert Nothing D. public A(){/*Code here*/} E. public A(){/*Code here*/} and public A(int i){/*Code*/} F. A(){/*Code here*/} and public A(int i){/*Code here*/} G. public A(){/*Code here*/} and A(int i){/*Code here*/} H. public A(){/*Code here*/} and protected A(int i){/*Code*/}

Question 73.
Which of the following statements are true? a. All of the variables in an interface are implicitly static b. All of the variables in an interface are implicitly final c. All of the methods in an interface are implicitly abstract d. A method in an interface can access class level variables

Institute of Java & Software Engineering

Question 74.
class Super{ private Super(){ System.out.print("super"); } } class Sub extends Super{ Sub(){ System.out.print("sub"); } } class Demo{ public static void main(String []args){ Sub ob=new Sub(); } } What is the result of attempting to compile and run the program? a. Prints: super b. Prints: sub c None of the Above d. Compiler Error

Question 77.
interface Vehicle{ /*insert code */ int commenID; //Line 45 } Which can be inserted at line 45 and still code to compile? a. final b. protected c. public d. static e. private f. abstract

Question 78.
interface Vehicle{ /*insert code */ int park(); //Line 45

} Which can be inserted at line 45 and still code to compile? a. final b. protected c. public d. static e. private f. abstract g. synchronized h. native

Question 75.
class Super{ public Super(){ System.out.print("super"); } } class Sub extends Super{ private Sub(){ System.out.print("sub"); } } class Demo{ public static void main(String []args){ Sub a=new Sub(); } } What is the result of attempting to compile and run the program? a. Prints: super b. Prints: sub c None of the Above d. Compiler Error

Question 79.
Which of the following statements are true? 1) A method in an interface must not have a body 2) A class may extend one other class plus at most one interface 3) A class may extends at most one other class plus implement many interfaces 4) An class accesses an interface via the keyword uses

Question 80.
Which of the following are legal declarations for nonnested classes and interfaces? a. final abstract class Test b. public static interface Test{} c. final public class Test{} d. protected abstract class Test{} e. protected interface Test{} f. abstract public class Test{}

Question 76.
class Super{ Super(){ System.out.print("super"); } } class Sub extends Super{ public Sub(){ System.out.print("sub"); } } class Demo{ public static void main(String []args){ Super a=new Super(); } } What is the result of attempting to compile and run the program? a. Prints: super b. Prints: sub c Compiler Error d None of the Above

Question 81.
Which of the following are true about interfaces. Select all correct answers? a. Methods declared in interfaces are implicitly private. b. Variables declared in interfaces are implicitly public, static, and final. c. An interface can extend any number of interfaces. d. The keyword implements indicate that an interface inherits from another.

Question 82.
Which of the following statements are true? a. Adding more classes via import statements will cause a performance overhead, only import classes you actually use. b. Under no circumstances can a class be defined with the private modifier c. A inner class can be defined with the protected modifier d. An interface cannot be instantiated

Institute of Java & Software Engineering

Question 83.
Given the folowing classes which of the following will compile without error? interface IFace{} class CFace implements IFace{} class Base{} public class ObRef extends Base{ public static void main(String argv[]){ ObRef ob = new ObRef(); Base b = new Base(); Object o1 = new Object(); IFace o2 = new CFace(); } } a. o1=o2; b. b=ob; c. ob=b; d. o1=b;

Question 86.

Question 84.
interface Lion{ int VALUE=1000; } interface Fox{ int VALUE=10; } class Man implements Lion,Fox{} class Demo{ public static void main(String args[]){ System.out.println(/*argument*/);//Line 23 } } Which of the following can be inserted at line 23 code to compile? A. VALUE B. Lion.VALUE C. Fox.VALUE D. Man.VALUE E. new Man().VALUE

f 1. interface I1 {} 2. interface I2 {} 3. class Base implements I1 {} 4. class Sub extends Base implements I2 {} 5. 6. class Red { 7. public static void main(String args[]) { 8. Sub s1 = new Sub(); 9. I2 i2 = s1; 10. I1 i1 = s1; 11. Base base = s1; 12. Sub s2 = (Sub)base; 13. } 14. } What is the result of attempting to compile and run the program? a. Compiler error at line 9. b. Compiler error at line 10. c. Compiler error at line 11. d. Compiler error at line 12. e. Runtime error. f. Compiles and runs without error.

Question 87.
1. interface I1 {} 2. interface I2 {} 3. class Base implements I1 {} 4. class Sub extends Base implements I2 {} 5. class Silver { 6. public static void main(String []args) { 7. Base[] base = {new Base()}; 8. Sub sub[] = new Sub[1]; 9. Object obj = base; 10. sub = (Sub[])obj; 11. I1 []i1 = (I1[])obj; 12. } 13. } What is the result of attempting to compile and run the program? a. Compiler error at line 9. b. Runtime error at line 9. c. Compiler error at line 10. d. Runtime error at line 10. e Runtime error at line 11. f. Compiles and runs without error

Question 85.
Given code fragment: interface Lion{ void roar(); void eat(); } class Animal{ void eat(){ //Code } } class Man extends Animal implements Lion{ public void roar(){ //Code } } //What is true? A. Overriding method eat() with "public" modifier in the class Man, the Code can be compiled B. Modifying eat() method in class Animal as "public void eat(){}", code can be compiled C. Removing eat() method of the interface Lion, the code can be compiled D. Removing eat() method of the class Animal, the code can be compiled E. The code will compile without any modification F. None of the above

Question 88.
1. interface I1 {} 2. interface I2 {} 3. class Base implements I1 {} 4. class Sub extends Base implements I2 {} 5. class Yellow { 6. public static void main(String args[]) { 7. Base base = new Sub(); 8. I1 i1 = base; 9. Sub sub = (Sub)base; 10. I2 i2 = (Sub)base; 11. } 12.} What is the result of attempting to compile and run the program? a. Compiler error at line 7. b. Runtime error at line 7. c. Compiler error at line 8. d. Runtime error at line 8. e Compiler error at line 9. f. Runtime error at line 9. g. Compiler error at line 10. h. Runtime error at line 10.

Institute of Java & Software Engineering

Question 89.
1. interface I1 {} 2. interface I2 {} 3. class Base implements I1 {} 4. class Sub extends Base implements I2 {} 5. class Orange { 6. public static void main(String args[]) { 7. Base base = new Base(); 8. I1 i1 = base; 9. Sub sub = (Sub)base; 10. } 11. } What is the result of attempting to compile and run the program? a. Compiler error at line 9. b. Runtime error at line 9. c. Compiler error at line 10. d. Runtime error at line 10. e. Compiles and runs without error

Question 92.
interface I{ } class A implements I{ } class D{ } class B{ public static void main(String args[]){ A a=new A(); A a1[]=new A[1]; // 1 I i i=new A(); // 2 Object ob=new Object(); ob=i; / /3 a1=(A[])ob; // 4 a=(A)i; // 5 D d=new D(); d=(D)i; // 6 a=(A)d; // 7 } } What is the result of attempting to compile and run the program? a. Compile time and run time error at line 3 b. Compile time ok and run time error at line 3 c. Compile time and run time error at line 4 d. Compile time ok and run time error at line 4 e. Compile time and run time error at line 5 f. Compile time ok and run time error at line 5 g. Compile time and run time error at line 6 h. Compile time ok and run time error at line 6 i. Compile time and run time error at line 7 j. Compile time ok and run time error at line 7

Question 90.
1. interface I1 {} 2. interface I2 {} 3. class Base implements I1 {} 4. class Sub extends Base implements I2 {} 5. class Gray { 6. public static void main(String []args) { 7. Base[] base = {new Base()}; 8. Sub sub[] = {new Sub()}; 9. Object obj = sub; 10. base = obj; 11. } 12. } What is the result of attempting to compile and run the program? a. Compiler error at line 7. b. Runtime error at line 7. c. Compiler error at line 8. d. Runtime error at line 8. e. Compiler error at line 9. f. Runtime error at line 9. g. Compiler error at line 10. h. Runtime error at line 10. i. Compiles and runs without error.

Question 93.
Given: 1. interface TestA { String toString(); } 2. public class Test { 3. public static void main(String[] args) { 4. System.out.println(new TestA() { 5. public String toString() { return test; } 6. }); 7. } 8. } What is the result? A. test B. null C. An exception is thrown at runtime. D. Compilation fails because of an error in line 1. E. Compilation fails because of an error in line 4. F. Compilation fails because of an error in line 5. Given: 10. class Line { 11. public class Point { public int x,y; } 12. public Point getPoint() { return new Point(); } 13. } 14. class Triangle { 15. public Triangle() { 16. // insert code here 17. } 18. } Which code, inserted at line 16, correctly retrieves a local instance of a Point object? a. Point p = Line.getPoint(); b. Line.Point p = Line.getPoint(); c. Point p = (new Line()).getPoint(); d. Line.Point p = (new Line()).getPoint();

Question 91.
1. interface I{ } 2. class A implements I{ } 3. class B{ 4. public static void main(String args[]){ 5. A a=new A(); 6. I i=new A(); 7. Object ob=new Object(); 8. ob=i; 9. a=i; 10. } 11.} What is the result of attempting to compile and run the program? a. Compiler error at line 6. b. Runtime error at line 6. c. Compiler error at line 8. d. Runtime error at line 8. e. Compiler error at line 9. f. Runtime error at line 9. g. Compiles and runs without error.

Institute of Java & Software Engineering

Question 94.
10. interface Foo { 11. int bar(); 12. } 13. 14. public class Beta { 15. 16. class A implements Foo { 17. public int bar() { return 1; } 18. } 19. 20. public int fubar( Foo foo) { return foo.bar(); } 21. 22. public void testFoo() { 23. 24. class A implements Foo { 25. public int bar() { return 2; } 26. } 27. 28. System.out.println( fubar( new A())); 29. } 30. 31. public static void main( String[] argv) { 32. new Beta().testFoo(); 33. } 34. } Which three statements are true? (Choose three.) a. Compilation fails. b. The code compiles and the output is 2. c. If lines 16, 17 and 18 were removed, compilation would fail. d. If lines 24, 25 and 26 were removed, compilation would fail. e. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2. f. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.

Question 96.
interface I{ String s="I"; } class A implements I{ String s="A"; } class B extends A{ String s="B"; } class C extends B{ String s="C"; void print(){ System.out.print(((A)this).s+((B)this).s+ ((C)this).s+((I)this).s); } public static void main(String args[]){ new C().print(); } } What is the result of attempting to compile and run the program? a. CCCC b. ABCC c. AAAA d. IABC e. ABCI f. Compiler error

Question 95.
Given: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException? a. Alpha a = x; b. Foo f= (Delta)x; c. Foo f= (Alpha)x; d. Beta b = (Beta)(Alpha)x;

Institute of Java & Software Engineering

Das könnte Ihnen auch gefallen