Java 5 SCJP Questions

10 02 2009

Interview Questions : Java 5 SCJP Questions

1. Given:
enum Horse {
PONY(10),
// insert code here
HORSE(15);

Horse(int hands) {
this.height = hands;
this.weight = hands * 100;
}
int height;
int weight;
int getWeight() { return weight; }
void setWeight(int w) { weight = w; }
}

class Stable {
public static void main(String [] hay) {
Horse h = Horse.ICELANDIC;
System.out.println(h.getWeight() + ” ” + h.height);
}
}

Which, inserted independently at ‘// insert code here’, produces the output:
800 13 ? (Choose all that apply.)

A). ICELANDIC(13) { weight = 800; },
B). ICELANDIC(13) { setWeight(800); },
C). ICELANDIC(13) { this.weight = 800; },
D). ICELANDIC(13) { public int getWeight() { return 800; } },
E). None of the above code will produce the specified output.
F). Because of other code errors, none of the above will compile.

2. Given:
1. class Voop {
2. public static void main(String [] args) {
3. doStuff(1);
4. doStuff(1,2);
5. }
6. // insert code here
7. }

Which, inserted at line 6, will compile? (Choose all that apply.)

A). static void doStuff(int… doArgs) { }
B). static void doStuff(int[] doArgs) { }
C). static void doStuff(int doArgs…) { }
D). static void doStuff(int… doArgs, int y) { }
E). static void doStuff(int x, int… doArgs) { }
F). None of the above code fragments will compile.

3. Given:
class Bird {
{ System.out.print(“b1 “); }
public Bird() { System.out.print(“b2 “); }
}
class Raptor extends Bird {
static { System.out.print(“r1 “); }
public Raptor() { System.out.print(“r2 “); }
{ System.out.print(“r3 “); }
static { System.out.print(“r4 “); }
}
class Hawk extends Raptor {
public static void main(String[] args) {
System.out.print(“pre “);
new Hawk();
System.out.println(“hawk “);
}
}

What is the result?

A). pre b1 b2 r3 r2 hawk
B). pre b2 b1 r2 r3 hawk
C). pre b2 b1 r2 r3 hawk r1 r4
D). r1 r4 pre b1 b2 r3 r2 hawk
E). r1 r4 pre b2 b1 r2 r3 hawk
F). pre r1 r4 b1 b2 r3 r2 hawk
G). pre r1 r4 b2 b1 r2 r3 hawk
H). The order of output cannot be predicted
I). Compilation fails

4. Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)
A). ClassCastException
B). IllegalStateException
C). NumberFormatException
D). IllegalArgumentException
E). ExceptionInInitializerError

5. Given:
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(long… x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
int doX(Number n, Number m) { return 4; }

public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + ” “);
System.out.println(doX(7,7));
}
}

What is the result?

A). 1 1
B). 2 1
C). 3 1
D). 4 1
E). 1 3
F). 2 3
G). 3 3
H). 4 3

6. Given:
import java.io.*;
class Player {
Player() { System.out.print(“p”); }
}
class CardPlayer extends Player implements Serializable {
CardPlayer() { System.out.print(“c”); }
public static void main(String[] args) {
CardPlayer c1 = new CardPlayer();
try {
FileOutputStream fos = new FileOutputStream(“play.txt”);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis = new FileInputStream(“play.txt”);
ObjectInputStream is = new ObjectInputStream(fis);
CardPlayer c2 = (CardPlayer) is.readObject();
is.close();
} catch (Exception x ) { }
}
}

What is the result?

A). pc
B). pcc
C). pcp
D). pcpc
E). Compilation fails
F). An exception is thrown at runtime

7. Given:

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

And the command line:

java Regex2 “d*” ab34ef

What is the result?

A). 234
B). 334
C). 2334
D). 0123456
E). 01234456
F). 12334567
G). Compilation fails

8. Given:
bw is a reference to a valid BufferedWriter

And the snippet:

15 BufferedWriter b1 = new BufferedWriter(new File(“f”));
16. BufferedWriter b2 = new BufferedWriter(new FileWriter(“f1″));
17. BufferedWriter b3 = new BufferedWriter(new PrintWriter(“f2″));
18. BufferedWriter b4 = new BufferedWriter(new BufferedWriter(bw));

What is the result?

A). Compilation succeeds
B). Compilation fails due only to an error on line 15.
C). Compilation fails due only to an error on line 16.
D). Compilation fails due only to an error on line 17.
E). Compilation fails due only to an error on line 18.
F). Compilation fails due to errors on multiple lines.

9. Using the fragments below, complete the following code so that it compiles and prints “40 36 30 28″. Note, you may use a fragment from zero to many times.

CODE:

import java.util.*;
class Pants { int size; Pants(int s) { size = s; } }
public class FoldPants {
List ________ p = new ArrayList ________ ();
class Comp implements Comparator ________ {
public int ________(Pants one, Pants two) {
return ________ – ________;
}
}
public static void main(String[] args) {
new FoldPants().go();
}
void go() {
p.add(new Pants(30));
p.add(new Pants(36));
p.add(new Pants(28));
p.add(new Pants(40));

Comp c = new Comp();
Collections. ________ ________;
for( ________ x : p)
System.out.print(________ + ” “);
}
}

FRAGMENTS:

Pants FoldPants Comp Comparator
compare compareTo sort order
one.size two.size x.size p.size
c.size (p, c) (c, p) sortList

10. 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;
H). package org.ex.why.Dog;

11. Given:
1. import java.util.*;
2. public class Fruits {
3. public static void main(String [] args) {
4. Set c1 = new TreeSet();
5. Set o1 = new TreeSet();
6. bite(c1);
7. bite(o1);
8. }
9. // insert code here
10. }
11. class Citrus { }
12. class Orange extends Citrus { }

Which, inserted independently at line 9, will compile? (Choose all that apply.)

A). public static void bite(Set s) { }
B). public static void bite(Set s) { }
C). public static void bite(Set s) { }
D). public static void bite(Set s) { }
E). public static void bite(Set s) { }
F). public static void bite(Set s) { }
G). Because of other errors in the code, none of these will compile.

Answers:

1. D is correct. (objective 1.1)

2. A and E use valid var-args syntax. (objective 1.1)
3. D is correct. Note: you’ll probably never see this many choices on the real exam! (objective 1.3)
4. B, C and D are correct. (objective 2.6)
5. H is correct. (objective 3.1)
6. C is correct. (objective 3.3)
7. E is correct. (objective 3.5)
8. B is correct. (objective 3.2)
9. The correct code is:

import java.util.*;
class Pants { int size; Pants(int s) { size = s; } }
public class FoldPants {
List p = new ArrayList();
class Comp implements Comparator {
public int compare(Pants one, Pants two) {
return two.size – one.size;
}
}
public static void main(String[] args) {
new FoldPants().go();
}
void go() {
p.add(new Pants(30));
p.add(new Pants(36));
p.add(new Pants(28));
p.add(new Pants(40));

Comp c = new Comp();
Collections.sort(p, c);
for(Pants x : p)
System.out.print(x.size + ” “);
}
}

(objective 6.5)

10. B, E, and G are required. (objective 7.5)
11. A, E, and F are correct uses of generics with (bounded) wildcards. (objective 6.4)





Manhattan Associates Placement Paper (EJB, JSP, JAVA & SQL)

10 02 2009

Paper: Manhattan Associates Placement Paper (EJB, JSP, JAVA & SQL)

EJB

1) What is true about ‘Primary Key class’ in Entity Beans ?

(a) Used to identify the entity bean based on EJB type, Home Interface and Container Context.
(b) Used to identify the entity bean based on EJB type, Remote Interface and Container Context.
(c) The definition of Primary Key class can be deferred till deployment

2) The Home Interface in Entity beans

(a) Provides at least one create() method
(b) May not provide any create() method
(c) Provides at least one findByPrimaryKey() method
(d) May not provide any findByPrimaryKey() method

3) In CMP of Entity beans

(a) Constructor with no arguments is defined
(b) Constructor is not defined

4) What is the purpose of ejbLoad()

5) What is the purpose of ejbStore()

6) In EJB, when a system error occurs, which exception is thrown ?

(a) EJBException
(b) RemoteException

7) In EJB, which of the following is an application level Exception ?

(a) NullPointerException
(b) ArrayOutOfBoundsException
(c) CreateException
(d) ObjectNotFoundException
(e) All the above
(f) None of the above

8) CMP bean provides

(a) Empty implementation of ejbLoad() and ejbStore()
(a) Concrete implementation of ejbLoad() and ejbStore()

JSP and Mislleneous

1) What is the purpose of XSL

(a) Convert XML to HTML
(b) Convert HTML to XML

2) resultSet has the following methods

(a) next()
(b) first()
(c) a & b
(d) No methods

3) In WebLogic clusters, what is the load balancing algorithm ?

(a) RoundRobin
(b) FIFO
(c) LIFO

4) How many Queues does a MDB listen to ?

(a) 1
(b) 2
(c) Any Number
(d) 10

5) Where is the Deployment Descriptor placed ?

(a) WEB-INF directory
(b) WEB-INF/CLASSES directory
(c) It will be mentioned in CLASSPATH
(d) The place can be specified in APPLICATION.xml

6) To denote distributed applications, What is the tag used in Deployment Descriptor ?

(a) distributable
(b) distributed=”true”
(c) both a & b

7) Can a JSP be converted to SERVLET and the vice versa always ?

(a) YES
(b) NO
8) Empty JSP Tag definitions are given in Deployment Descriptor. Then which of the following syntaxes are correct ?

9) One small question on tag

JAVA

1) Which of the following 2 methods executes faster ?

class Trial {
String _member;

void method1() {
for(int i=0;i<2048;i++) {
_member += “test”;
}
}

void method2() { String temp;

for(int i=0;i<2048;i++) {
temp += “test”;
}
_member = temp;
}

}
(a) method1()
(b) method2()
(c) Both method1() and method2() takes same time for execution

2) Integer.parseInt(“12a”) returns
(a) Exception
(b) 1
(c) 0
(d) -1

3) By default, Strings to functions are passed using the method

(a) Call by Value
(b) Call by Reference
(c) Strings cannot be passed to function

4) What is the difference between OVERRIDING and OVERLOADING

5) What is the output of following program ?class Test {
public static void main(String args[]) {
for(int i=0;i<2;i++) {
System.out.println(i–);
}
}
}

(a) Goes into infinite loop
(b) 0,1
(c) 0,1,2
(d) None

6) ‘t’ is the reference to a class that extends THREAD. Then how to suspend the execution of thisthread ?
(a) t.yield()
(b) yield(t)
(c) yield(0
(d) yield(100) where 100 is the milli seconds of time

7) What is the functionality of instanceOf() ?
8) How many String objects are created by the following statements ?

String str = ” a+b=10 “;
trim(str)
str.replace(+,-);
(a) 1
(b) 2
(c) 3
(d) 4

9) An ABSTRACT class is declared and the code is tried to instantiate it. The Question was whetherit’s legal to do it or not ?

10) A question on “interface”

11) Cleaning operation in Java is done in the method

(a) finally()
(b) finalize()
(c) final()

12) Question on whether Static method can be overriden

13) How to prevent a class from being the Base Class ?

(a) declare it as final
(b) declare it as static

14) If we want to read a very big text file with so many mega bytes of data, what shall we use ?

(a) FileInputStream
(b) InputStreamReader
(c) BufferedReader

15) One Question on Inner Classes.

16) One program on Overloading and Overriding

17) A program given using try, catch and finally and it is asked to find out which statements getexecuted ?

18) What code, if written, below the (//code here) will display 0.

class {
public static void main(String argv[]) {
int i=0;

//code here
}
}
(a) System.out.println(i++)
(b) System.out.println(i+’0′)
(c) System.out.println(i–)
(d) System.out.println(i)

19) What is the better way of writing the Constructor with 2 parameters in the following code:class Test {

int x,y;

Test(int a) {

//Code for very complex operations will be written
//in this place

x=a;
}

Test(int a, int b) {

//Code for very complex operations will be written
//in this place (same code as in above constructor)

x=a;
y=b;
}
}

SQL

1) 2 Table Structures are given. (Something like Employee ID, Salary, Department etc)And there are 3 queries. I remember one query.
Write query to find the employees with top 5 salaries ?

2) What is the difference between UNION and UNION ALL ?

3) What is the difference between COMMIT and ROLLBACK ?