Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
The assessment consists of part A and part B. Each question in part A may
contain multiple answers and each question carries 1 mark. Part B
contains 2 programming questions, and each question carries 5 marks.
Don’t write anything on this question paper, write all the answers in a
separate white paper and indicate your name and the Java Assessment
set (from above) in the answer paper.
Question 1
Given:
31. // some code here
32. try {
33. // some code here
34. } catch (SomeException se) {
35. // some code here
36. } finally {
37. // some code here
38. }
Under which three circumstances will the code on line 37 be executed?
(Choose three.)
A. The instance gets garbage collected.
B. The code on line 33 throws an exception.
C. The code on line 35 throws an exception.
D. The code on line 31 throws an exception.
E. The code on line 33 executes successfully.
Question 2
12. Given:
13. public class Pass {
14. public static void main(String [1 args) {
15. int x 5;
16. Pass p = new Pass();
17. [Link](x);
18. [Link](” main x = “+ x);
19. }
20.
21. void doStuff(int x) {
22. [Link](” doStuff x = “+ x++);
23. }
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
24. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuffx = 6 main x = 6
D. doStuffx = 5 main x = 5
E. doStuffx = 5 main x = 6
F. doStuffx = 6 main x = 5
Question 3
Given:
10. class Nav{
11. public enum Direction { NORTH, SOUTH, EAST, WEST }
12. }
13. public class Sprite{
14. // insert code here
15. }
Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. [Link] d = NORTH;
C. Direction d = [Link];
D. [Link] d = [Link];
Question 4
Given:
11. public static void parse(String str) {
12. try {
13. float f= [Link](str);
14. } catch (NumberFormatException nfe) {
15. f= 0;
16. } finally {
17. [Link](f);
18. }
19. }
20. public static void main(String[] args) {
21. parse(”invalid”);
22. }
What is the result?
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
A. 0.0
B. Compilation fails.
C. A ParseException is thrown by the parse method at runtime.
D. A NumberFormatException is thrown by the parse method at runtime.
Question 5
Given:
11. public enum Title {
12. MR(”Mr.”), MRS(”Mrs.”), MS(”Ms.”);
13. private final String title;
14. private Title(String t) { title = t; }
15. public String format(String last, String first) {
16. return title + “ “ + first + “ “ + last;
17. }
18. }
19. public static void main(String[] args) {
20. [Link]([Link](”Doe”, “John”));
21. }
What is the result?
A. Mr. John Doe
B. An exception is thrown at runtime.
C. Compilation fails because of an error in line 12.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 20.
Question 6
Given:
10. public class MyClass {
11.
12. public Integer startingI;
13. public void methodA() {
14. Integer i = new Integer(25);
15. startingI = i;
16. methodB(i);
17. }
18. private void methodB(Integer i2) {
19. i2 = [Link]();
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
20.
21. }
22. }
If methodA is invoked, which two are true at line 20? (Choose two.)
A. i2 == startingI returns true.
B. i2 == startingI returns false.
C. [Link](startingI) returns true.
D. [Link](startingI) returns false.
Question 7
Given:
11. public static void test(String str) {
12. int check = 4;
13. if (check = [Link]()) {
14. [Link]([Link](check -= 1) +“, “);
15. } else {
16. [Link]([Link](0) + “, “);
17. }
18. }
and the invocation:
21. test(”four”);
22. test(”tee”);
23. test(”to”);
What is the result?
A. r, t, t,
B. r, e, o,
C. Compilation fails.
D. An exception is thrown at runtime.
Question 8
Which two are true? (Choose two.)
A. A finalizer may NOT be invoked explicitly.
B. The finalize method declared in class Object takes no action.
C. [Link]() is called implicitly by any overriding finalize method.
D. The finalize method for a given object will be called no more than
once by the garbage collector.
E. The order in which finalize will be called on two objects is based on
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
the order in which the two objects became finalizable.
Question 9
Given:
1. public class GC {
2. private Object o;
3. private void doSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9.o=null;
10. }
11. }
When the doSomething method is called, after which line does the Object
created in line 5 become available for garbage collection?
A. Line 5
B. Line 6
C. Line 7
D. Line 8
E. Line 9
F. Line 10
Question 10
Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10; i++) {
14. int value = i * ((int) [Link]());
15. Integer intObj = new Integer(value);
16. [Link](intObj);
17. }
18. [Link](numbers);
19. }
Which line of code marks the earliest point that an object referenced by
intObj becomes a candidate for garbage collection?
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.
Question 11
Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x= 1; x<[Link]; x++) {
16. [Link](yahoo[x] + “ “);
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A.a b
B.b c
C.a b c
D. Compilation fails.
E. An exception is thrown at runtime.
Question 12
Given a class Repetition:
1. package utils;
2.
3. public class Repetition {
4. public static String twice(String s) { return s + s; }
5. }
and given another class Demo:
1. // insert code here
2.
3. public class Demo {
4. public static void main(String[] args) {
5. [Link](twice(”pizza”));
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
6. }
7. }
Which code should be inserted at line 1 of [Link] to compile and
run Demo to print “pizzapizza”?
A. import utils.*;
B. static import utils.*;
C. import [Link].*;
D. static import [Link]. *;
E. import [Link]();
F. import static [Link];
G. static import [Link];
Question 13
Given:
int[] myArray=newint[] {1, 2,3,4, 5};
What allows you to create a list from this array?
A. List myList = [Link]();
B. List myList = [Link](myArray);
C. List myList = new ArrayList(myArray);
D. List myList = [Link](myArray);
Question 14
Given:
11. public class Key {
12. private long id1;
13. private long 1d2;
14.
15. // class Key methods
16. }
A programmer is developing a class Key, that will be used as a key in a
standard [Link]. Which two methods should be overridden to
assure that Key works correctly as a key? (Choose two.)
A. public int hashCode()
B. public boolean equals(Key k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(Key k)
Answer: AD
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
Question 15
Given:
10. public class ClassA {
11. public void count(int i) {
12. count(++i);
13. }
14. }
And:
20. ClassA a = new ClassA();
21. [Link](3);
Which exception or error should be thrown by the virtual machine?
A. StackOverflowError
B. NullPointerException
C. NumberFormatException
D. IllegalArgumentException
E. ExceptionlnlnitializerError
Question 16
Below are the sql tables.
Employee
Employee_ Last First Location_id Emplo Salary Statu
id Nam Nam yment s
e e _type_i
d
1 LN1 FN1 1 1 100000 A
2 LN2 FN2 2 2 80000 R
3 LN3 FN3 2 1 100000 A
4 LN4 FN4 2 2 40000 A
5 LN5 FN5 2 2 50000 A
Location
Location_id Location_name
1 Chennai
2 Kolkata
Rapid Care Transcription Pvt Ltd
Java Assessment (Set A)
Employment_type
Employment_type_id Employment_type_desc
1 Permanent
2 Contract
Write a program to connect to this database and provide the list of
resigned contract employees with salary less than 50,000 sorted by their
location.
Question 17:
There is list of employees in a text file.
Each employee is separated by comma(,).
The order of the columns is employee id, last name, first name,
salary and joining date.
Write a program to read a list of employees from a text file, eliminate
duplicates, sort them by salary, then joining date and finally print the
sorted list to the console.