Aptitude MCQ For Beginners

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

1.

Suppose you are creating a class named Button that you want to include in a
group of related classes called controls.
Identify the correct code that includes the class in that group.
 package controls;
 public class Button
 package Button;
 import controls;

2. Given the following


class A{
public int foo;
}
public class B extends A{
private int bar;
public void setBar(int b){
bar=b;
}
}
which is true about the classes described above?
 Class A is tightly encapsulated.
 Class B is tightly encapsulated.
 Classes A and B are both tightly encapsulated.
 Neither class A nor B is tightly encapsulated.

3. Testing an individual unit of code is known as _____________


 Integration testing
 Functional Testing
 Accepting Testing
 Unit Testing

4. Given the following

public class RTExcept{


public static void throwit(){
System.out.print("throwit");
throw new RuntimeException();
}
public static void main(String [ ] args) {
try
{
System.out.print("hello");
throwit();
}
catch(Exception re){
System.out.print("caught");
}
finally{
System.out.print("finally");
}
System.out.print("after");
}
}
 hello throwit caught
 hello thowitRuntimeException caught after
 hello throwit caught finally after
 hello throwit caught finally after RuntimeException

5. Given the following

1. class Animal{
2. String name="No name";
3. public Animal(String nm){name=nm;}
4. }
5.
6. class DomesticAnimal extends Animal{
7. String animalFamily = "nofamily";
8. public DomesticAnimal(String family){ animalFamily = family;}
9. }
10.
11. public class AnimalTest{
12. public static void main(String args[]){
13. DomesticAnimal da=new DomesticAnimal("cat");
14. System.out.println(da.animalFamily);
15. }
16. }
What is the result?
 Cat
 Nofamily
 An exception is throw at runtime.
 Compilation falis due to an error in line 8.

6. Which of the annotation in Junit 4.x, does the role of class wide setup
method of previous versions?
 @Before
 @BeforeClass`
 @Test
 None of the above

7. Which of the annotation in Junit 4.x, does the role of class wide teardown
method of previous versions?
 @Before
 @AfterClass
 @Test
 None of the above

8. Given the following

class Base{
Base(){
System.out.println("Base constructor invoked..");
}
}
public class Derived extends Base{
Derived(){
System.out.println("Dervied constructor invoked..");
}

public static void main(String args[]){


Base b=new Dervied();
}
}
 Base constructor invoked.
Derived constructor invoked.
 Derived constructor invoked.
 Derived constructor invoked.
Base constructor invoked..
 No output is produced.

9. If x, y and z are all integers, which expression will produce a runtime error?
NOTE: The expressions are always evaluated with all the integers having a
value of 1.
 z = x/y--;
 z = -x/x;
 z = y/x--;
 z = y%--x;

10. Which of the following is a unit testing frameword?


 ANT
 Junit
 Eclipse
 None of the above

11. what is the output for the below code?


public class Test{
public static void main(String args[]){
Integer i=null;
int j=i;
System.out.println(j);
}
}
 0
 Compile time error
 NullPointerException
 Null

12. Given the following code:


public class ArithmeticTest{
public static void main(String args[]){
try
{
int x=0;
int y=5/x;
System.out.println(y);
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException");
}
}
}
what is the output?
 Exception
 ArithmeticException
 NaN
 Compile time error

13. assertEquals() of Junit 4.x doesn’t use autoboxing


 True
 False

14. Which is true about the package statement in java?


 It can appear anywhere in the file as long as the syntax is correct
 It should appear after all the import statements but before the class
declaration
 There can be more than one package statement
 It should be the first non-comment line in the Java source file.

15. import java.io.*;


public class Test{
public static void main(String args[]){
try
{

InputStreamReaderisr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
}
catch(IOException e){
e.printStackTrace();
}
}
}
 Compile time error
 Always throws an exception during runtime
 Compiles and reads one line of input from the console
 Reads one line at a time till we press ‘q’

16. Given the following code:


class Dog{
Dog(String name){}
}
If class Beagle extends Dog and class Beagle has only one constructor,
which of the following could be the legal constructor for class Beagle?
 Beagle(){}
 Beagle(){super();}
 Beagle(){super(“fido”);}
 No constructor, allow the default constructor to get generated automatically.

17. Which statement is TRUE about the try{} block?


 It is mandatory for statements in a try{} block to throw at least one
exception type
 The statements in a try{} block can only throw one exception type and not
several types
 The try{} block can contain loops or branches.
 The try{} block can appear after the catch() blocks

18. public class ExceptionTest{


public static void main(String args[]){
try
{
ExceptionTest a=new ExceptionTest();
a.badMethod();
System.out.println("A");
}
catch(Exception e)
{
System.out.println("B");
}
finally
{
System.out.println("C");
}
}
}
 BC followed by Error exception
 Error exception followed by BC
 C followed by Error exception
 Error exception followed by C

19. Which one of the following statement is false?


 A subclass must override all the methods of the superclass.
 It is possible for a subclass to define a method with the same and parameters
as a method defined by the superclass.
 Aggregation defines a has – a relationship between a superclass and its
subclasses.
 Inheritance defines a is-a relationship between a superclass and its
subclasses

20. Streams are classified as ____________ and ___________ streams


 Character and Byte
 Bit and word
 Bit and Byte
 Character and word

21. What will be the result of attempting to compile and run the following class?
public class Passing{
public static void main(String args[]){
int a=0; int b=9;
int[] bArr=new int[1]; bArr[0]=b;
inc1(a);inc2(bArr);
System.out.println("a="+a+"b="+b+"bArr[0]="+bArr[0]);
}

public static void inc1(int x){ x++; }


public static void inc2(int[] x){ x[0]++; }
}
select the correct answer.
 The code will compile and will print “a=1 b=10 bArr[0]=10” when run
 The code will compile and will print “a=0 b=10 bArr[0]=10” when run
 The code will compile and will print “a=0 b=9 bArr[0]=10” when run
 The code will compile and will print “a=0 b=10 bArr[0]=0” when run

22. Which one of the following statements is true?


 An abstract class can be instantiated
 An abstract class is implicitly final.
 An abstract class can declare non-abstract methods.
 An abstract class can not extend a concrete class.

23. What command in the Java 2 SDK should be used to compile the following
code contained in a file called HelloWorld.java?
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");
}
}
 java HelloWorld
 javacHelloWorld
 java HelloWorld.java
 javac HelloWorld.java

24. public class Test{


public static void main(String[] args){
System.out.println(6^4);
}
}
what is the output?
 1296
 24
 2
 Compilation error

25. Can an object of a child type be assigned to a variable of the parent type?
For example,
Card crd;
BirthDaybd=new BirthDay(“Lucinda”,42);
Crd=bd; // is this correct?
 No-there must always be an exact match between the variable and the object
types.
 No-but a object of parent type can be assigned to a variable of child type.
 Yes-an object can be assigned to a reference variable of the parent type.
 Yes-any object can be assigned to any reference variable.

26. Examine the following code:


String str="Hot Java";

boolean bValue=strinstanceOf String;


what value is placed in bValue?
 True
 False
 “Hot Java”
 Null

27. Which is true about the static import statement in Java?


 It is used when one wants to use a class’s static members.
 It is the same as import statement
 When static import statement is used in a Java source file, the classes
declared in that file cannot declare any non-static members.
 The statement can be written either as “static import” of “import static”. It
does not matter how it is written.

28. Here is a method definition:


int compute(int a, double y){…..}
which of the following has a different signature?
 int compute(int sum, double value){…}
 double compute(int a, double y){….}
 double compute(int sum, double y){….}
 int compute(int a, int y){….}

29. Which of the following lists exception types from MOST specific to LEAST
specific?
 Error, Exception
 Exception, RunTimeException
 Throwable, RunTimeException
 ArithmeticException, RunTimeException
30. class A{
public static void main(String[] args) {
Error error=new Error();
Exception exception =new Exception();
System.out.println((exception instanceOfThrowable)+ ",");
System.out.println(error instanceOfThrowable);
}}
what is the result of attempting to compile and run the program?
 Prints: false,false
 Prints: false,false
 Prints: true,false
 Prints: true, true

31. class TestPoly{


public static void main(String[] args) {
Parent p=new Child();
}
}
class Parent{
public Parent(){
super();
System.out.println("instantiate a parent");
}
}
class Child extends Parents{
public Child(){
System.out.println("instaniate a child");
}
}
 instantiate a child
 instantiate a parent
 instantiate a child
instantiate a parent
 instantiate a parent
instantiate a child

32. public class Constructor{


public Constructor(int x, int y, int z)
{}
}
which of the following is considered as overloaded constructor?
 Constructor() {}
 protected int Constructor(){}
 private Object Constructor(){}
 public void Constructor(int x, int y, byte z){}

33. Consider the code below:


int arr[][]=new int[3][];
arr[0]=new int[7];
arr[1]=new int[4];
arr[2]=new int[2];
for(int n=0;n<3;n++)
System.out.println(/* what goes here? */);

which statement below, when inserted as the body of the for loop,
would print the number of values in each row?
 arr[n].length
 arr[n].length()
 arr[n].size
 arr[n].size()

34. Given the following:


class Vehicle{}

class FourWheeler extends Vehicle{}

class Car extends FourWheeler{}

public class TestVehicle


{
public static void main(String[] args)
{
Vehicle v=new Vehicle();
FourWheeler f=new FourWheeler();
Car c=new Car();

xxxxxxx
}
}
Which of the following statement is legal, which can be substituted for
xxxxxxx?
 v=c;
 c=v;
 f=v;
 c=f;

35. Which of the following statement is legal, which can be substituted for
xxxxxxx?
If MyProg.java were compiled as an application and then run from the
command line as
java MyProg I like myprogram

What would be the value of args[1] inside the main() method?


 MyProg
 I
 like
 Myprogram

36. Assume that val has been defined as an int for the code below:
if(val>4)
{System.out.println("Test A");
}
else if(val>9)
{System.out.println("Test B");
}
elseSystem.out.println("Test C");
Which values of val will result in “Test C” NOT being printed?
 val<0
 val=0
 o<val<4
 4<val<9

37. Given the following:


public class MyProgram{
public static void main(String args[]){
try{
System.out.println("HelloWorld");
}
finally{
System.out.println("Finally executing");
}
}
}
What is the result?
 Nothing. The program will not compile because no exceptions are specified.
 Nothing. The program will not compile because no catch clauses are
specified.
 Hello world.
 Hello world Finally executing.

38. Suppose you have four int variables: x,y,z and result.
Which expression sets the value of z to x if result has a value of 1, and the
value of y to x otherwise?
 x=(result==1)?z:y;
 x=(result==1)?y:z;
 x=(result==1):y?z;
 x=(result==1):z?y;

39. Which one of the below statement is true?


 When a class has defined constructors with parameters, the compiler does
not create a ---- no—args constructor
 When a constructor is provided in a class a corresponding destructor should
also be provided
 The compiler always creats the default no-args constructor for every class
 The no-args constructor can invoke only the no-args constructor of the
superclass. It cannot invokes any other constructor of the superclass

40. Assume Card is the base class of Valentine, Holiday and Birthday, in order
for the following code to be correct,
what must be the type of the reference variable card?
___________ card;

card=new Valentine("Joe",14);
card.greeting();

card=new Holiday("Bob");
card.greeting();

card=new Birthday("Emily",12);
card.greeting();
 Valentine
 Holiday
 Birthday
 Card

41. Given:
public interface Alpha{
String MESSAGE="Welcome";
public void display();
}
to create an interface called Beta that has Alpha as its parent, which
interface declaration is correct?
 public interface Beta extends Alpha{}
 public interface Beta implements Alpha{}
 public interface Beta instanceOf Alpha{}
 public interface Beta parent Alpha{}

42. Which of the field declaration is legal within the body of an interface?
 protected static int answer=42;
 int answer;
 int answer=42;
 private final static int answer=42;

43. Given classes A,B and C, where B extends A, and C extends B, and where
all classes implement the instance method void doIt(). How can the doIt()
method in A be called from an instance method in C?
 super.doIt();
 super.super.doIt();
 A.this.doIt();
 It is not possible.

44. Which of the following is not a wrapper class?


 String
 Float
 Double
 Integer
45. What will be the output of the below program: public class Demo { public
static void main(String args[]) { for(int counter=1; counter<20; counter++){
if(counter>10) { break; } else{ System.out.print(counter); }}}}
 12345678910111213141516171819
 1234567891011121314151617181920
 12345678910
 123456789

46. Annotation is a feature added in ____java.Lang.Annotation____ package


of Java.

47. What does RetentionPolicy.CLASS indicate?


 These annotations will be retained only at the source level and will be
ignored by the compiler.
 These annotations will be retained only by the VM so they can be read only
at run-time.
 These annotations will be retained by the compiler at compile time, but
will be ignored by the VM
 None of the above

48. What is a method’s signature?


 The signature of a method is the name of the method and the type of its
return value.
 The signature of a method is the name of the method and the names of its
parameters.
 The signature of a method is the name of the method and the data type of its
parameters.
 The signature of a method is the name of the method, its parameter list,
and its return type.

49. Which of the following is true?


 A Java program will execute faster than an equivalent program written in
C++.
 The bytecode for a particular Java program will be the same for
different platforms
 Java can only be used to create browser based applications.(i.e. applets)
 Java was developed by Microsoft

50. Given the following,


1. class MyInherit{
2. int calculate(int m,float n){
3. return 9;
4. }
5. }
6.
7.class MyInheritChild extends MyInherit{
8. // insert code here
9. }
which method if inserted ar line 8, will NOT compile?
 private int calculate(int a, float b){return 25;}
 Private int calculate(int a, double b){return 25;}
 public int calculate(int a, float b){return 25;}
 protected int calculate(int a, float b){return 25;}

51. interface I1{


void draw();
}
class C implements I1{
xxxxxx
}

Which of the following when inserted at xxxxxx is a legal definition and


implementation?
 void draw() {}
 public void draw(){}
 protected void draw(){}
 abstract void draw(){}

52. Given:
1. abstract class MyClass{
2. void init() { }
3. abstract int calculate();
4. }
5. class MyImpl extends MyClass{
6. int calculate(){
7. System.out.println("Invoking calculate....");
8. return 1;
9. }
10. }
11. public class TestMyImpl{
12. public static void main(String[] args){
13. MyImpl mi=new MyImpl();
14. mi.init();
15. mi.calculate();
16. }
17. }
 Prints “Invoking calculate…”
 Runtime error occurs.
 Compilation error at line 2.
 Compilation error at line 14.

53. The @ Test annotation is used to indicate


 A test method
 That the corresponding function should get execute before each test
method
 A test class
 None of the above

54. Say that class Rodent has a child class Rat and another child class Mouse.
Class Mouse has a child class PocketMouse. Examine the following
Rodent rod;
Rat rat=new Rat();
Mouse mos=new Mouse();
PocketMousepkt=new PocketMouse();
Which of he following array declations is correct for an array that is
expected to hold upto 10 objects of types Rat, Mouse and PocketMouse?
 Rat[] array =new Rat[10];
 Rodent[] array=new Rat[10];
 Rodent[] array=new Rodent[10];
 Rodent[10] array;

55. Which statement is true?


 Public methods of a superclass cannot be overridden in subclasses.
 Protected methods of a superclass cannot be overridden in subclassese
 Methods with default access in a superclass cannot be overridden in
subclasses
 Private methods of a superclass cannot be overridden in subclasses.

56. What type parameter must the following method be called with?
int myMethod(double[] ar)
{
….
}
 An empty double array
 A reference to an array that contains elements of type double
 A reference to an array that contains zero or more elements of type int.
 An array of length that contains double and must be named ar.

57. What will happen to the following test? Will it fail or pass?
@Test(timeout=100)
public void infinity()
{
while(true);
}
 Pass
 Fail

58. What is output of following program


class ABC
{
public static void main(String a[])
{
Double d=new Double(“Wipro”);
System.out.println(d.doubleValue());
}
}
 Compilation error
 Throws NumberFormatException at runtime
 Throws InputMismatchException at runtime
 0
59. Given the following code, which statement is true?
public interface HeavenlyBody{ String describe();}
class Star implements HeavenlyBody{
String starName;
public String describe(){return "star" + starName;}
}
class Planet{
String name;
Star orbiting;
public String describe(){
return "planet"+name+"orbiting" + orbiting.describe();
}
}
 The code will fail to compile
 The use of aggregation is justified, since Planet has-a Star
 The code will fail to compile if the name starName is replaced with the
name bodyName throughout the declaration of the Star class.
 An instance of Planet is a valid instance of a HeavenlyBody.

60. Can an abstract class define both abstract methods and non-abstract
methods?
 No-it must have all one or the other
 No-it must have all abstract methods
 Yes-but the child classes do not inherit the abstract methods
 Yes-the child classes inherit both

61. What statement is true?


 a super() or this() call must always be provided explicitly as the first
statement in the body of a constructor
 If the both a subclass and its superclass do not have any declared
constructors, the implicit default constructor of the subclass will call super()
when run.
 If neither super() nor this is decalred as the first statement in the body of a
constructor, then this() will implicitly be inserted as the first statement
 If super() is the first statement in the body of a constructor, then this() can be
declared as the second statement.

62. Which will legally declare, construct, and initialize an array?


 int [] myList={“9”,”6”,”3”};
 int [3] myList=(9,6,3);
 int myList [][]={9,6,3,0};
 int [] myList={9,6,3};

63. what is output of following program


class YY
{
void m1(Integer i1)
{
System.out.println("Interger:"+i1.intValue());}
void m1(int i1)
{
System.out.println(i1);}
}
class A
{
public static void main(String arg[])
{
YY y1=new YY();
y1.m1(3);
}
}
 3 This is answer
 Integer:3
 Compilation error
 Runtime error

64. What will be the output of the below program:


public class Demo {
public static void main(String args[]) {
int[] myArray={1,2,3,4,5};
for(int counter:myArray){
System.out.println(counter++);
}
}
}
 2
3
4
5
 1
2
3
4
5
 No Output
 Compilation Error

65. Given the following code:


public class test {
public static void main(String args[]) {
System.out.println(args.length);
}
}
If the above code is compiled and run as follows
java Test Hello 1 2 3

What would be the output?


 6
 5
 4 This is answer
 3

66. Given the following classes anddeclartions, which statement is true?

//Classes
class Foo{
private int i;
private void f(){/*.. */}
public void g(){/*...*/}
}

class Bar extends Foo{


public int j;
public void g(){/".."}
}

//declarations
//....
Foo a =new Foo();
Bar b=new Bar();
// ...
 The statement b.f(); is legal
 The statement a.j=5; is legal
 The statement a.g(); is legal
 The statement b.i=3, is legal

67. _________ is used for reading objects from files


 ObjectStream
 ObjectInputStream
 ObjectReader
 FileReader

68. Which of the following are the optional parameter of Junit @Test
annotation?
 Expected
 Timeout
 Both expected and timeout
 None of the above

69. Given:
class Friut{
private String name;
public Fruit(String name){this.name=name;}
public String getName(){return name;}
}
public classMyFruit extends Fruit{
public void displayFruit(){}
}
which of the following statement is true?
 The code will compile if public MyFruit(){Fruit();} is added to the MyFruit
class.
 The code will compile if public MyFruit(){Fruit();} is added to the Fruit
class.
 The code will compile if public Fruit(){this(“apple”);} is added to the Fruit
class.
 The code will compile if public Fruit(){Fruit(“apple”);} is added to the
Fruit class.

70. class Test{


static boolean b1;
public static void main(String[] args){
boolean[] array=new boolean[1];
boolean b2=true;
System.out.print(b1+",");
System.out.print(array[0]+",");
System.out.print(b2);
}
}
what is the result of attempting to compile and run the program?
 Prints: true, true, true
 Prints: false, false, true
 Prints: false, null, true
 Compile-time error

71. Identify the correct signature of the main method of a Java application
 public void main(String[] args)
 public static void main(String args)
 public static int main(String args[])
 public static void main(String[] args)

72. given the following program, which statement is true?

class MyClass{

public static void main(String[] args){


String[] numbers={"one", "two", "three", "four"};
if(args.length==0){
System.out.println("no arguments");
}
else{
System.out.println(numbers[args.length]+"arguments");
}
}
}
 The program will print “no argument” and “three arguments” when called
with zero and three program arguments, respectively.
 The program will print “no argument” and “four arguments” when
called with zero and three program arguments, respectively.
 The program will print “one argument” and “four arguments” when called
with zero and three program arguments, respectively.
 The program will throw a NullPointerException when run with zero
program arguments.

73. Junit is a ____________ framework?


 Integration testing
 Functional testing
 Acceptance testing
 Unit testing

74. In Junit, it is possible to ignore a test method form testing?


 True
 False

75. What is a CLASSPATH?


 An environment variable which is used by the Java compiler to look for the
Java source files.
 An environment variable which is used by the Java Compiler and the
JVM to look for dependent Java class files.
 An environment variable which stores the path of the Java compiler.
 An environment variable which stores the path of the Java interpreter.

76. Which of these statement is true?


 Finally block gets executed only when there are exception
 Finally gets always executed irrespective of the flow in try catch block
 Finally block can be present only when a catch block is present
 Finally block gets executed only when there are no exceptions.

77. You want to create a table that looks like


12 -9 8
7 14
-32 - 1 0
Which of the following is a correct and legal representation of the above
table?
 double[][] table={12, -9, 8, 7, 14, -32, -1, 0};
 double[][] table={{12, -9, 8},{7, 14,0},{ -32, -1, 0}};
 double[3][] table={{12, -9, 8},{7, 14},{ -32, -1, 0}};
 double[][] table={{12, -9, 8},{7, 14},{ -32, -1, 0}};

78. What will happen if you try to compile and run the following code?
int a=200;
byte b=a;
System.out.println(“The value of b is “+b);
 It will compile and print The value of b is 200
 It will compile but cause an error at runtime
 Compile- time error
 It will compile and print The value of b is -56
79. Which of the following Annotations are used to mark if a function is
obsolete?
 @SuppressWarnings
 @Override
 @Deprecated
 @Inherited

80. Superclass of all classes representing an input stream of bytes is


_____________
 InputStream
 Writer
 Reader
 OutputStream

81. What is the value of seasons.length for the following array?


String[] seasons={“winter”, ”spring”, ”summer”, ”fall”};
 Undefined
 4
 5
 22

82. Integer ten =new Integer(10);


Long nine =new Long(9);
System.out.print(ten+nine);

What is output of above code


 Ten nine
 19
 Compilation error
 Runtime error

83. Given the following ,


class TestFooBar{
public static Foo f=new Foo();
public static Foo f2;
public static Bar b=new Bar();

public static void main(String [] args) {


for(int x=0;x<4;x++){
f2=getFoo(x);
f2.react();
}
}
static Foo getFoo(int y){
if(0==y%2){
return f;
}
else {
return b;
}
}
}
 Bar BarBarBar
 Foo b Bar Foo Bar
 Foo FooFooFoo
 Compilation fails.

84. We can create our own user defined Annotations


 True
 False

85. 1. abstract class AbstractIt


2. {
3. abstract float getFloat();
4. }
5. public class Test1 extends AbstractIt
6. {
7. private float f1=1.0f;
8. private float getFloat(){return f1;}

9. public static void main(String[] args)


10. {
11. }
12. }
 Compilation error at line no 5
 Runtime error at line 8
 Compilation error at line no 8
 Compilation succeeds

86. A constructor is used to


 Free memory
 Initialize a newly created object
 Import packages
 Clean up the object

87. what does the following methods do?


void blur(char[] z, String st)
{
if (z.length<st.length()) return;
for (int j=0;j<st.length();j++)
z[j]=st.char(j);
}
 a) it determines if the array contains the same characters as the string
 b) it copies characters from the array to the string
 c) it creates a new array that contains the same characters as the string
 d) if there are enough slots in the array. it copies characters one by one
from the string to the

88. given the following code


public class Test{
public static void main(String args[]){
System.out.println(args[0]);
}
}
if the above code is compiled and run as follows
java Test hello 1 2 3
what would be the output?
 a) java
 b) test
 c) hello
 d) hello 1 2 3

89. which statement 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

90. given the following
1. long test(int x, float y) {
2
3 }
which one of the following line inserted at line 2 would not compile?
 a) return (long) y;
 b) return (int) 3.14d;
 c) return (y/x);
 d) return x/7;

91. which statement is true about interfaces?


 a) interface allows multiple implementation inheritance
 b) interfaces can extend any number of other interfaces.
 c) members of an interface are never static
 d) members of an interface can always be declared static.

92. which is a valid declartion within an interface?


 a) protected short stop = 23; NOT VALID
 b) final void madness(short stop);
 c) public Boolean madness(long bow);
 d) static char madness(double duty);

93.
given the following
1) class Dog
2) Dog(String name) { }
3)
If class Beagle extends Dog and class Beagle has only one constructor,
which of the following could be the legal contructor for the class Beagle?
 a) Beagle(){}
 b) Beagle(){super();}
 c) Beagle(){super ("fido@”);}
 d) no Constructor, allow the default constructir

94. If x,y and z are all integer, which expression will produce a runtime error?
Note: The expressions are always evaluated with all integers having a value
of 1.
 z=x/y - - ;
 z=-x/x;
 z= y/x - -
 z=y% - - x

95. When is a finally{} block executed?

 Only when an unhandled exception is thrown in a try{}


 Only when any exception is thrown in a try{] block.
 Always after execution has left a try catch{} block, no matter for what
reason
 Always just as a method is about to finish

96. Which of the following statement is true?


 A subclass must override all the methods of the superclass
 It is possible for a subclass to define a method with the same name and
parameters d defined by the superclass.
 Aggregation define a has-a relationship between a superclass and its
subclasses.
 Inheritance defined a is- a relationship between a superclass and its
subclasses.

97. Given:
abstract class Shape{
public abstract void draw();
}
public class Circle extends Shape{
public void draw(){}
}
which one of the following statement is correct?
 Shape s=new Shape();
s.draw();
 Circle c = new Shape();
c.draw();
 Shape s=new Circle();
s.draw();
 Shape s=new Circle();
s→draw();

98. Given the following code
public class Demo{
public static void main(Sring[] args[]){
B b=new B();
D d =new D();
B bd=new D();
System.out.println(b.m+""+d.m+""+bd.m);
}
}

class B{ int m=7;}


class D extends B{int m=9;}
what will be the output on executing the above code?

 797
 799
 979
 997

99. Given the following


class Dogs{
public String toString(){
return "theDogs";
}
}
public class TestDogs{
public static void main(String [] args){
Dogs [][] dogs=new Dogs[3][];
System.out.println(dogs[2][0].toString());
}
}
What is the result?
 Null
 TheDogs
 Compilation fails
 NullPointerException is thrown at runtime

100. public class Constructor{


public Constructor(int x, int y, int z)
{
}
}
which of the following is considered as overloaded constructor?
 constructor(){}
 protected int constructor(){}
 private object construcot(){}
 public void constructor(int x, int y, int z){}

101. what will be the result of attempting to compile and run the following
program?
public class Polymorphism{
public static void main(Sring[] args[]){
A ret1=new C();
B ret2 =(B)ret1;
System.out.println(ret2.f());
}
}

class A{int f(){ return 0; } }


class B extends A{int f() { return 1;} }
class C extends B{int f() { return 2;} }

 The program will fail to compile


 The program will compile without error, but will throw a
ClassCastException
 The program will compile without error and print 1 when run
 The program will compile without error and print 2 when run

102. Which of the following is an illegal declaration of array?


 int [] myscore[];
 char [] mychars;
 Dog mydogs[7];
 Dog mydogs[];

103. What is an abstract method?


 An abstract method is any method in an abstract class
 An abstract method is a method which cannot be inherited
 An abstract method is one without a body that is declared with the reserve
 An abstract method is a method in the child class that overrides a parent

104. 1. public class TestFloatDouble{
2. public static void main(Sring[] args[]){
3. float f1=2.0f;
4. double d1=4.0;
5. double result=f1*d1;
6. System.out.println(result);
7. }
8. }
what is the output?
 8.0
 Compilation error at Line 3
 Compilation error at Line 4
 Compilation error at Line 5

105. Which declaration prevents creating a subclass of a top level class?


 private class javacg{}
 abstract public class javacg{}
 final public class javacg{}
 final abstract class javacg{}
106. What is CLASSPATH?
 An environment variable which is used by the Java compiler to look for the
Java
 An environment variable which is used by the Java compiler and the
JVM to look for dependent Java class files.
 An environment variable which stores the path of the Java compiler.
 An environment variable which stores the path of the Java interpreter.

107. Say that class Rodent has a child class Rat and another child class Mouse.
Class Mouse has a child class PocketMouse. Examine the following
Rodent rod;
Rat rat=new Rat();
Mouse mos=new mouse();
PocketMouse pkt =new new PocketMouse();
Which of the following array declarations is correct for an array that is
expected to hold up to 10 objects of types Rat, Mouse and PocektMouse?
 Rat[] array=new Rat[10];
 Roden[] array =new Rat[10];
 Rodent[] array =new Rodent [10];
 Rodent[10] array;

108. Which one of the following is legal declaration for nonnested classes and
interfaces?
 final abstract class test{}
 public static interface test{}
 final public class test{}
 protected interface test{}

109. Given the following


1. interface DoMath{
2. double getArea(int rad); }

3. interface MathPlus{
4. double getVol(int b, int h); }
5.
6.
7.
8.
Which code fragment inserted at lines 7 and 8 will compile?
 class AllMath extends DoMath { double getArea(int r); }
 interface AllMath implements MathPlus{double getVol(int x, int y); }
 interface AllMath extends DoMath{float getAvg(int h, int i);}
 class allmath implements MathPlus { double getArea (int rad);}

110. Given the following


1. Class MySuper{
2. public MySuper(int){
3. System.out.println("Super" + i);
4. }
5. }
6.
7. public class MySub extends MySuper {
8. public MySub() {
9. super(2);
10. System.out.println("sub");
11.}
12.
13. public static void main(String args[])
14. MySuper sup=new MySub();
15. }
16 }
what is the result?

 Sub
Super 2
 Super 2
Sub
 Compilation fails at line 9.
 Compilation fails at line 14

111. Which statement is TRUE about catch{} blocks?
 There can only be one catch{} block in a trycatch structure
 The catch{} block for a child exception class must PRECEDE that of a
parent exception
 The catch{} block for a child exception class must FOLLOW that of a
parent exception
 A catch {} block need not be present even if there is no finally{ } block

112. Which of the following lists exception types from MOST specific to
LEAST specific?

 Error, Exception
 Exception, RunTimeException
 Throwable, RunTimeException
 ArithmeticException, RunTimeException

113. Which one of the below statement is true?

 When a class has defined constructors with parameters, the compiler does
not create a ---- no—args constructor
 When a constructor is provided in a class a corresponding destructor should
also be provided
 The compiler always creats the default no-args constructor for every
class
 The no-args constructor can invoke only the no-args constructor of the
superclass. It cannot invokes any other constructor of the superclass
114. Given:
public class Employee{
private String empID;
public String empName;
private Integer arge;

public void setEmployeeInfo(String empId, String empName, Integer age)


{
this.empId=empId;
this.empName=empName;
this.age=age;
}
}
which is true?
 The class is fully encapsulated
 The empName variable breaks encapsulation
 The empId and age variables break polymorphism
 The setEmployeeInfo method breaks encapsulation

115. What will be the output of the below program: public class Demo { public
static void main(String args[]) int variable1=2; int variable2=3; for(int
counter=1; counter<10 & variable1>1; counter++){ if(counter%2 ==0) {
System.out.print(counter); } else{ variable1++; System.out.print(variable1);
}}}}
 32445668
 32446687
 324456687
 32456687

116. After the declaration:


char[] c=new char[100];
what is the value of c[50]?

 49
 50
 ‘u0020’
 ‘u0000’

117. class A{A(int i) {} } /1
class B extends A { } //2
which one of the following statement is correct?

 The compile attempts to create a default constructor for -----


 Compile – time error at 1
 Compile – time error at 2
 Compiles successfully without any errors.

118. Given:
interface I1{ }
class A implements I1{}
class B extends A{}

class C extends B{
public static void main(string args[]){
B b=new B();
XXXXXXXXX // insert statement here
}
}
which code, inserted at xxxxxxxx, will cause a
java.lang.ClassCastException?
 A a=b;
 I1 i=(C)b;
 I1 i=(A)b;
 B b2=(B)(A)b;

You might also like