Aptitude MCQ For Beginners
Aptitude MCQ For Beginners
Aptitude MCQ For Beginners
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;
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
class Base{
Base(){
System.out.println("Base constructor invoked..");
}
}
public class Derived extends Base{
Derived(){
System.out.println("Dervied constructor invoked..");
}
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;
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’
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]);
}
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
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.
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
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()
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
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
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;
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.
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.
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;
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
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
//Classes
class Foo{
private int i;
private void f(){/*.. */}
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
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.
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)
class MyClass{
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
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
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);
}
}
797
799
979
997
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());
}
}
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{}
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);}
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
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;
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
49
50
‘u0020’
‘u0000’
117. class A{A(int i) {} } /1
class B extends A { } //2
which one of the following statement is correct?
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;