[go: up one dir, main page]

0% found this document useful (0 votes)
71 views16 pages

UttaraMockTests FirstTest

This document contains 25 coding questions and multiple choice answers related to Java programming. The questions cover topics like variable scope, object references, operators, control flow, exceptions, and more. The full document tests knowledge of Java syntax, semantics, and best practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views16 pages

UttaraMockTests FirstTest

This document contains 25 coding questions and multiple choice answers related to Java programming. The questions cover topics like variable scope, object references, operators, control flow, exceptions, and more. The full document tests knowledge of Java syntax, semantics, and best practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

!

!
Uttara InfoSolutions
www.uttarainfo.com
!
First Test Questions (40Q - 50 Mins)
!
1) What will be printed out if this code is run with the following command line?
!
java myprog good morning
!
public class myprog
{
public static void main(String argv[])
{
System.out.println(argv[2]);
}
}
1) myprog
2) good
3) morning
4) Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"
!
2) Which of the following are legal identifiers
!
1) 2variable
2) variable2
3) _whatavariable
4) _3_
5) $anothervar
6) #myvar
!
Options:
a) 1,2,3,4
b) 2,3,4,5
c) 3,4,5,6
d) 1,3,4,6
!
3) What will happen when you compile and run the following code?

public class MyClass


{
static int i;
public static void main(String argv[])
{
System.out.println(i);
}
}
1) Error Variable i may not have been initialized
2) null
3) 1
4) 0
!
4)
What will happen if you try to compile and run the following code?
!
public class Q
{
public static void main(String argv[])
{
int anar[]=new int[5];
System.out.println(anar[0]);
}
}
1) Error: anar is referenced before it is initialized
2) null
3) 0
4) 5
!
5) What will happen if you try to compile and run the following code?
!
public class Q {
public static void main(String argv[])
{
Integer anar[]=new Integer[5];
System.out.println(anar[0]);
}
}
1) Error: anar is referenced before it is initialized
2) null
3) 0
4) 5
!
6) What will be printed out if you attempt to compile and run the following code ?
!
int i=1;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
default:
System.out.println("default");
}
1) one
2) one, default
3) one, two, default
4) default
!
7)
What will happen if you attempt to compile and run the following code?
!
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
!
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception
4) No Exception
!
8)
Which of the following statements are true?
!
1) System.out.println( -1 >>> 2);will output a result larger than 10
2) System.out.println( -1 >>> 2); will output a negative number
3) System.out.println( 2 >> 1); will output the number 1
4) System.out.println( 1 <<< 2); will output the number 4
5) System.out.println( 1 << 32); will output the number 2
6) System.out.println( 1 <<< -2); will throw error
!
Options
a) 1,2,3
b) 1,3,4
c) 2,3,5
d) 1,3,5
!
9)
What will be output by the following code?
!
public class MyFor{
public static void main(String argv[]){
int i;
int j;
outer:
for (i=1;i <3;i++)
inner:
for(j=1; j<3; j++) {
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
}
}
!
}
1) Value for i=1 Value for j=1
2) Value for i=2 Value for j=1
3) Value for i=2 Value for j=2
4) Value for i=3 Value for j=1
!
Options:
a) 1,3
b) 2,3
c) 3,4
d) 1,2
!
10)
Which of the following will output -4.0
!
1) System.out.println(Math.floor(-4.7));
2) System.out.println(Math.round(-4.7));
3) System.out.println(Math.ceil(-4.7));
4) System.out.println(Math.min(-4.7));
!
11)
If you run the code below, what gets printed out?
!
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));
1) Bic
2) ic
3) icy
4) error: no method matching substring(int,char)
!
12)
What will be the result when you attempt to compile and run the following code?.
!
public class Conv{
public static void main(String argv[]){
Conv c=new Conv();
String s=new String("ello");
c.amethod(s);
}
!
public void amethod(String s){
char c='H';
c+=s;
System.out.println(c);
}
}
!
1) Compilation and output the string "Hello"
2) Compilation and output the string "ello"
3) Compilation and output the string elloH
4) Compile time error
!
13)
Given the following code what will be output?
!
public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
!
public void amethod(int x){
x=x*2;
j=j*2;
}
}
1) Error: amethod parameter does not match variable
2) 20 and 40
3) 10 and 40
4) 10, and 20
!
14)
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);
}
}
!
1) Error at compile time
2) 200
3) 100 followed by 200
4) 100
!
15)
What will happen when you try compiling and running this code?
!
public class Ref{
public static void main(String argv[]){
Ref r = new Ref();
r.amethod(r);
}
public void amethod(Ref r){
int i=99;
multi(r);
System.out.println(i);
}
public void multi(Ref r){
r.i = r.i*2;
}
}
1) Error at compile time
2) An output of 99
3) An output of 198
4) An error at runtime
!
16)
What will be the result when you attempt to compile this program?
!
public class Rand{
public static void main(String argv[]){
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
!
1) Compile time error referring to a cast problem
2) A random number between 1 and 10
3) A random number between 0 and 1
4) A compile time error
!
17)
You are concerned that your program may attempt to use more memory than is available.
To avoid this situation you want to ensure that the Java Virtual Machine will run its garbage
collection just before you start a complex routine. What can you do to be certain that
garbage collection will run when you want .
!
1) You cannot be certain when garbage collection will run
2) Use the Runtime.gc() method to force garbage collection
3) Ensure that all the variables you require to be garbage collected are set to null
4) Use the System.gc() method to force garbage collection
!
18)
What will happen when you attempt to compile and run the following code
!
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
!
!
!
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
!
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}
1) Compile time error complaining that Base.amethod is private
2) Runtime error complaining that Base.amethod is private
3) Output of "Base.amethod"
4) Output of "Over.amethod"
!
19)
What will happen if you try to compile and run the following code
!
public class MyClass {
public static void main(String arguments[]) {
amethod(arguments);
}
public void amethod(String[] arguments) {
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
1) error Can't make static reference to void amethod.
2) error method main not correct
3) error array must include parameter
4) amethod must be declared with String
!
20)
What will happen when you attempt to compile and run the following code
!
public class As{
int i = 10;
int j;
char z= 1;
boolean b;
public static void main(String argv[]){
As a = new As();
a.amethod();
}
public void amethod(){
System.out.println(j);
System.out.println(b);
}
}
1) Compilation succeeds and at run time an output of 0 and false
2) Compilation succeeds and at run time an output of 0 and true
3) Compile time error b is not initialised
4) Compile time error z must be assigned a char value
!
21)
What will happen when you attempt to compile and run the following code
!
public class StrEq{
public static void main(String argv[]){
StrEq s = new StrEq();
}
private StrEq(){
String s = "Marcus";
String s2 = new String("Marcus");
if(s == s2){
System.out.println("we have a match");
}else{
System.out.println("Not equal");
}
}
}
1) Compile time error caused by private constructor
2) Output of "we have a match"
3) Output of "Not equal"
4) Compile time error by attempting to compare strings using ==
!
22)
What will be output by the following line?
!
System.out.println(Math.floor(-2.1));
1) -2
2) 2.0
3) -3
4) -3.0
!
23)
What will happen when you attempt to compile and run the following code
!
int Output=10;
boolean b1 = false;
if((b1==true) && ((Output+=10)==20))
{
System.out.println("We are equal "+Output);
}
else
{
System.out.println("Not equal! "+Output);
}
1) Compile error, attempting to peform binary comparison on logical data type
2) Compilation and output of "We are equal 10"
3) Compilation and output of "Not equal! 20"
4) Compilation and output of "Not equal! 10”
!
24)
What will be output by the following line of code?
!
System.out.println(010|4);
!
1) 14
2) 0
3) 6
4) 12
!
25)
Given the following code what will be the output?
!
class ValHold{
public int i = 10;
}
!
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i );
}//End of amethod
!
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}//End of another
!
}
1) 10030
2) 20030
3) 209930
4) 10020
!
26)
What is the output for the below code ?
public class C {
!
}
public class D extends C{
!
}
public class A {
public C getOBJ(){
System.out.println("class A - return C");
return new C();

}
!
}
public class B extends A{

public D getOBJ(){
System.out.println("class B - return D");
return new D();
}
}
!
public class Test {
!
public static void main(String... args) {
A a = new B();
a.getOBJ();
}
}
!
Options
A)Compile with error - Not allowed to override the return type of a method with a subtype
of the original type.
B)class A - return C
C)class B - return D
D) Runtime Exception
!
27)
Given:

5. class Atom {

6. Atom() { System.out.print("atom "); } 7. }

8. class Rock extends Atom {



9. Rock(String type) { System.out.print(type); }

10. }

11. public class Mountain extends Rock {

12. Mountain() {

13. super("granite ");

14. new Rock("granite ");

15. }

16. public static void main(String[] a) { new Mountain(); } 17. }

What is the result?

A. Compilation fails.

B. atom granite

C. An exception is thrown at runtime.

D. atom granite atom granite

28)

Given:

11. abstract class Vehicle { public int speed() { return 0; }

12. class Car extends Vehicle { public int speed() { return 60; } 13. class RaceCar extends
Car { public int speed() { return 150; } ...

21. RaceCar racer = new RaceCar();

22. Car car = new RaceCar();

23. Vehicle vehicle = new RaceCar();

24. System.out.println(racer.speed() + ", " + car.speed()

25. + ", " + vehicle.speed());

What is the result?

A. 0, 0, 0

B. 150, 60, 0

C. Compilation fails.

D. 150, 150, 150

29)

Given:

5. class Building { }

6. public class Barn extends Building { 7. public static void main(String[] args) { 8. Building
build1 = new Building();

9. Barn barn1 = new Barn();

10. Barn barn2 = (Barn) build1;

11. Object obj1 = (Object) build1;

12. String str1 = (String) build1;

13. Building build2 = (Building) barn1; 14. }

15. }

Which is true?

A. If line 10 is removed, the compilation succeeds.



B. If line 11 is removed, the compilation succeeds.

C. If line 12 is removed, the compilation succeeds.

D. If line 13 is removed, the compilation succeeds.

E. More than one line must be removed for compilation to succeed.

30)

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;

31)

Given:

1. public class Boxer1{

2. Integer i;

3. int x;

4. public Boxer1(int y) {

5. x = i+y;

6. System.out.println(x);

7. }

8. public static void main(String[] args) { 9. new Boxer1(new Integer(4));

10. }

11. }

What is the result?

A. The value "4" is printed at the command line.


B. B. Compilation fails because of an error in line 5.
C. C. Compilation fails because of an error in line 9.
D. D. A NullPointerException occurs at runtime.
32)

Given:

3. public class Batman {

4. int squares = 81;

5. public static void main(String[] args) {

6. new Batman().go();

7. }

8. void go() {

9. incr(++squares);

10. System.out.println(squares);

11. }

12. void incr(int squares) { squares += 10; }

13. }

What is the result?

A. 81 B. 82 C. 91 D. 92
33)

Given:

3. interface Animal { void makeNoise(); }

4. class Horse implements Animal {

5. Long weight = 1200L;

6. public void makeNoise() { System.out.println("whinny"); }

7. }

8. public class Icelandic extends Horse {

9. public void makeNoise() { System.out.println("vinny"); }

10. public static void main(String[] args) {

11. Icelandic i1 = new Icelandic();

12. Icelandic i2 = new Icelandic();

12. Icelandic i3 = new Icelandic();

13. i3 = i1; i1 = i2; i2 = null; i3 = i1;

14. }

15. }

When line 14 is reached, how many objects are eligible for the garbage collector?

A. 0

B. 1

C. 4

D. 3

34)

Given:

1. public class A {

2. public void doit() { 3. }

4. public String doit() {



5. return "a";

6. }

7. public double doit(int x) { 8. return 1.0;

9. }

10. }

What is the result?

A. An exception is thrown at runtime.



B. Compilation fails because of an error in line 7.

C. Compilation fails because of an error in line 4.

D. Compilation succeeds and no runtime errors with class A occur.

35)

Given:

11. public static void main(String[] args) { 12. for (int i = 0; i <= 10; i++) {

13. if (i > 6) break;

14. }

15. System.out.println(i);

16. }

What is the result?

A. 6

B. 7

C. 10

D. Compilation fails.

!
36)

What is the output (Assuming written inside main)


String s1 = new String("amit");
String s2 = s1.replace('m','i');
s1.concat("Poddar");
System.out.println(s1);
System.out.println((s1+s2).charAt(5));
!
a) Compile error
b) amitPoddar
o
c) amitPoddar
i
d) amit
i
!
37)
What is the output (Assuming written inside main)
String s1 = new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
System.out.println(s3==s4);
!
a) arit
amit
false
true
b) arit
arit
false
true
c) amit
amit
false
true
d) arit
amit
true
true
!
38)
What is the output of following (Assuming written inside main)
String s1 = "Amit";
String s2 = "Amit";
String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(s1.equals(s2));
System.out.println((s1==s2));
System.out.println(s3.equals(s4));
System.out.println((s3==s4));
a) true
true
true
false
b) true
true
true
true
c) true
false
true
false
!
39)

11. class Alpha {



12. public void foo() { System.out.print("Afoo "); }

13. }

14. public class Beta extends Alpha {



15. public void foo() { System.out.print("Bfoo "); }

16. public static void main(String[] args) {



17. Alpha a = new Beta();

18. Beta b = (Beta)a;

19. a.foo();

20. b.foo();

21. }

22. }

What is the result?

A. Afoo Afoo

B. Afoo Bfoo

C. Bfoo Afoo

D. Bfoo Bfoo

40)
Given:

3. class Fish { }

4. class Perch extends Fish { }

5. class Walleye extends Perch { }

6. class Bluegill { }

7. public class Fisherman {



8. public static void main(String[] args) {

9. Fish f = new Walleye();

10. Walleye w = new Walleye();

11. Bluegill b = new Bluegill();

12. if(f instanceof Perch)

System.out.print("f-p ");

13. if(w instanceof Fish)

System.out.print("w-f ");

14. if(b instanceof Fish)

System.out.print("b-f "); 15. }



16. }


What is the result?

A. w-f

B. f-p w-f

C. w-f b-f

D. f-p w-f b-f

You might also like