Lara technologies
Section1: Each Question 1 Mark
1. What all gets printed when the following code is compiled and run?
Select the three correct answers.
public class M3 {
public static void main(String args[]) {
for(int i = 0; i < 2; i++) {
for(int j = 2; j>= 0; j--) {
if(i == j) break;
[Link]("i=" + i + " j="+j);
}
}
}
}
A. i=0 j=0
B. i=0 j=1
C. i=0 j=2
D. i=1 j=0
E. i=1 j=1
F. i=1 j=2
G. i=2 j=0
H. i=2 j=1
I. i=2 j=2
2. What gets printed when the following code is compiled and run with the
following command -
java M3 2
public class M3 {
public static void main(String args[]) {
Integer intObj=[Link](args[[Link]-1]);
int i = [Link]();
if([Link] > 1)
[Link](i);
if([Link] > 0)
[Link](i - 1);
else
[Link](i - 2);
}
3. What all gets printed when the following program is compiled and run.
Select the two correct answers.
public class M3 {
public static void main(String args[]) {
int i, j = 1;
i = (j > 1) ? 2 : 1;
switch(i) {
case 0: [Link](0); break;
case 1: [Link](1);
case 2: [Link](2); break;
case 3: [Link](3); break;
}
}
}
A. 0
B. 1
C. 2
D. 3
4.
public class M3 {
public static void main(String args[]) {
int i = 0, j = 2;
do {
i = ++i;
j--;
} while(j > 0);
[Link](i);
}
}
5. What all gets printed when the following gets compiled and run. Select
the three correct answers.
public class M3 {
public static void main(String args[]) {
int i = 1, j = 1;
try {
i++;
j--;
if(i / j > 1)
i++;
}
catch(ArithmeticException e) {
[Link](0);
}
catch(ArrayIndexOutOfBoundsException e) {
[Link](1);
}
catch(Exception e) {
[Link](2);
}
finally {
[Link](3);
}
[Link](4);
}
A. 0
B. 1
C. 2
D. 3
E. 4
6. What all gets printed when the following gets compiled and run. Select
the two correct answers.
public class M3 {
public static void main(String args[]) {
int i=1, j=1;
try {
i++;
j--;
if(i == j)
i++;
}
catch(ArithmeticException e) {
[Link](0);
}
catch(ArrayIndexOutOfBoundsException e) {
[Link](1);
}
catch(Exception e) {
[Link](2);
}
finally {
[Link](3);
}
[Link](4);
}
}
A. 0
B. 1
C. 2
D. 3
E. 4
7. What all gets printed when the following gets compiled and run. Select
the two correct answers.
public class M3 {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
[Link](1);
else
[Link](2);
if([Link](s2))
[Link](3);
else
[Link](4);
}
}
A. 1
B. 2
C. 3
D. 4
8. At what stage in the following method does the object initially referenced
by s becomes available for garbage collection. Select the one correct
answer.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
A. Before statement labeled 1
B. Before statement labeled 2
C. Before statement labeled 3
D. Before statement labeled 4
E. Never.
9. If a base class has a method defined as
void method() { }
Which of the following are legal prototypes in a derived class of this
class. Select the two correct answers.
a. void method() { }
b. int method() { return 0;}
c. void method(int i) { }
d. private void method() { }
10.
public class M3 {
public static void main(String args[]) {
int i;
float f = 2.3f;
double d = 2.7;
i = ((int)[Link](f)) * ((int)[Link](d));
[Link](i);
}
}
11.
public class M3 {
public static void main(String args[]) {
String str1="abc";
String str2="def";
String str3=[Link](str2);
[Link](str2);
[Link](str1);
}
}
12.
public class Test {
public static void main(String [] args) {
int x = 5;
boolean b1 = true;
boolean b2 = false;
if ((x == 4) && !b2 )
[Link]("1 ");
[Link]("2 ");
if ((b2 = true) && b1 )
[Link]("3 ");
}
}
13.
public class M3 {
static int i = 1;
public static void main(String args[]) {
int j = test(i++ + i + test(i++ + i) + i + test(i++ + i) + i);
[Link](i);
[Link](j);
}
static int test(int i) {
return i++ + M3.i++;
}
}
14.
public class M3 {
static int i = 1;
public static void main(String args[]) {
int j = i | i++ & i++ ^ i++ & i;
[Link](i);
[Link](j);
}
}
15.
public class Threads2 implements Runnable {
public void run() {
[Link]("run.");
throw new RuntimeException("Problem");
}
public static void main(String[] args) {
Thread t = new Thread(new Threads2());
[Link]();
[Link]("End of method.");
}
}
Which two can be results? (Choose two.)
A. [Link]: Problem
B. run.
[Link]: Problem
C. End of method.
[Link]: Problem
D. End of method.
run.
[Link]: Problem
E. run.
[Link]: Problem
End of method.
16.
public class TestSeven extends Thread {
private static int x;
public synchronized void doThings() {
int current = x;
current++;
x = current;
}
public void run() {
doThings();
}
}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access
problems.
E. Declaring the doThings() method as static would make the class
thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new
Object()) { } block would make the class
thread-safe.
17.
public class TestOne implements Runnable {
public static void main (String[] args) throws Exception {
Thread t = new Thread(new TestOne());
[Link]();
[Link]("Started");
[Link]();
[Link]("Complete");
}
public void run() {
for (int i = 0; i < 4; i++) {
[Link](i);
}
}
}
What can be a result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "StartedComplete".
D. The code executes and prints "StartedComplete0123".
E. The code executes and prints "Started0123Complete".
18.
1. public class TestFive {
2. private int x;
3. public void foo() {
4. int current = x;
5. x = current + 1;
6. }
7. public void go() {
8. for(int i = 0; i < 5; i++) {
9. new Thread() {
10. public void run() {
11. foo();
12. [Link](x + ", ");
13. } }.start();
14. } }
Which two changes, taken together, would guarantee the output: 1,
2, 3, 4, 5, ? (Choose two.)
A. move the line 12 print statement into the foo() method
B. change line 7 to public synchronized void go() {
C. change the variable declaration on line 2 to private volatile int x;
D. wrap the code inside the foo() method with a synchronized( this )
block
E. wrap the for loop code inside the go() method with a synchronized
block synchronized(this)
{ // for loop code here }
19. A
public class TwoThreads {
private static Object resource = new Object();
private static void delay(long n) {
ry { [Link](n); }
catch (Exception e) { [Link](”Error “); }
}
public static void main(String[] args) {
[Link](”StartMain “);
new Thread1().start();
delay(1000);
Thread t2 = new Thread2();
[Link]();
delay(1000);
[Link]
delay(1000);
[Link](”EndMain “);
}
static class Thread 1 extends Thread {
public void run() {
synchronized (resource) {
[Link](”Startl “);
delay(6000);
[Link](”End1 “);
}
}
}
static class Thread2 extends Thread {
public void run() {
synchronized (resource) {
[Link](”Start2 “);
delay(2000);
[Link](”End2 “);
}
}
}
}
Assume that sleep(n) executes in exactly m milliseconds, and all
other
code executes in an insignificant amount of time. What is the
output if
the main() method is run?
A. Compilation fails.
B. Deadlock occurs.
C. StartMain Start1 Error EndMain End1
D. StartMain Start1 EndMain End1 Start2 End2
E. StartMain Start1 Error Start2 EndMain End2 End1
F. StartMain Start1 Start2 Error End2 EndMain End1
G. StartMain Start1 EndMain End1 Start2 Error End2
20.
public class Messager implements Runnable {
public static void main(String[] args) {
new Thread(new Messager("Wallace")).start();
new Thread(new Messager("Gromit")).start();
}
private String name;
public Messager(String name) { [Link] = name; }
public void run() {
message(1); message(2);
}
private synchronized void message(int n) {
[Link](name + "-" + n + " ");
}
}
Which of the following is a possible result? (Choose all that apply.)
A. Wallace-1 Wallace-2 Gromit-1
B. Wallace-1 Gromit-2 Wallace-2 Gromit-1
C. Wallace-1 Gromit-1 Gromit-2 Wallace-2
D. Gromit-1 Gromit-2
E. Gromit-2 Wallace-1 Gromit-1 Wallace-2
F. The code does not compile.
G. An error occurs at run time.
21.
public class Letters extends Thread {
private String name;
public Letters(String name) { [Link] = name; }
public void write() {
[Link](name);
[Link](name);
}
public static void main(String[] args) {
new Letters("X").start();
new Letters("Y").start();
}}
We want to guarantee that the output can be either XXYY or YYXX,
but never XYXY or any other combination. Which of the following
method definitions could be added to the Letters class to make this
guarantee? (Choose all that apply.)
A. public void run() { write(); }
B. public synchronized void run() { write(); }
C. public static synchronized void run() { write(); }
D. public void run() { synchronized(this) { write(); } }
E. public void run() { synchronized([Link]) { write(); } }
F. public void run() { synchronized([Link]) { write(); } }
G. public void run() { synchronized([Link]) { write(); } }
22.
public class M3 {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<String>();
[Link]("aAaA");
[Link]("AaA");
[Link]("aAa");
[Link]("AAaa");
[Link](strings);
for (String s : strings) {
[Link](s + " ");
}
}
}
23.
interface A {
void x();
}
class B implements A {
public void x() {
[Link]("B-X");
}
public void y() {
[Link]("B-Y");
}
}
class C extends B {
public void x() {
[Link]("C-X");
}
}
public class M3 {
public static void main(String[] args) {
[Link]<A> list = new [Link]<A>();
[Link](new B());
[Link](new C());
for (A a : list) {
a.x();
a.y();
}
}
}
24.
public class Score implements Comparable<Score> {
private int wins, losses;
public Score(int w, int 1) { wins = w; losses = 1; }
public int getWins() { return wins; }
public int getLosses() { return losses; }
public String toString() {
return “<“ + wins + “,“ + losses + “>”;
}
// insert code here
}
Which method will complete this class?
A. public int compareTo(Object o) {/*mode code here*/}
B. public int compareTo(Score other) {/*more code here*/}
C. public int compare(Score s1,Score s2){/*more code here*/}
D. public int compare(Object o1,Object o2){/*more code here*/}
25.
import [Link].*;
class KeyMaster {
public int i;
public KeyMaster(int i) { this.i = i; }
public boolean equals(Object o) { return i == ((KeyMaster)o).i;
}
public int hashCode() { return i; }
}
public class MapIt {
public static void main(String[] args) {
Set<KeyMaster> set = new HashSet<KeyMaster>();
KeyMaster k1 = new KeyMaster(1);
KeyMaster k2 = new KeyMaster(2);
[Link](k1); [Link](k1);
[Link](k2); [Link](k2);
[Link]([Link]() + “:”);
k2.i = 1;
[Link]([Link]() + “:”);
[Link](k1);
[Link]([Link]() + “:”);
[Link](k2);
[Link]([Link]());
}
}
26.
1. import [Link].*;
2. public class Test {
3. public static void main(String[] args) {
4. List<String> strings = new ArrayList<String>();
5. // insert code here
6. }
7. }
Which four, inserted at line 5, will allow compilation to succeed?
(Choose four.)
A. String s = [Link](0);
B. Iterator i1 = [Link]();
C. String[] array1 = [Link]();
D. Iterator<String> i2 = [Link]();
E. String[] array2 = [Link](new String[1]);
F. Iterator<String> i3 = [Link]<String>();
27.
1. public class Test {
2. public <T extends Comparable> T findLarger(T x, T y) {
3. if([Link](y) > 0) {
4. return x;
5. } else {
6. return y;
7. }
8. }
9. }
and:
22. Test t = new Test();
23. // insert code here
Which two will compile without errors when inserted at line 23?
(Choose two.)
A. Object x = [Link](123, “456”);
B. int x = [Link](123, new Double(456));
C. int x = [Link](123, new Integer(456));
D. int x = (int) [Link](new Double(123), new Double(456));
28.
ArrayList a = new ArrayList();
containing the values {“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”}
Which code will return 2?
A. Collections. sort(a, [Link]());
int result = [Link](a, “6”);
B. Comparator c = [Link]();
[Link](a, c);
int result = [Link](a, “6”);
C. Comparator c = [Link]();
[Link](a, c);
int result = [Link](a, “6”,c);
D. Comparator c = [Link](a);
[Link](a, c);
int result = [Link](a, “6”,c);
E. Comparator c = new InverseComparator(new Comparator());
[Link](a);
int result = [Link](a, “6”,c);
29.
class Test {
public static void main(String[] args) {
// insert code here
[Link]("one");
[Link]("two");
[Link]("TWO");
[Link]([Link]());
}
}
Which, inserted at // insert code here, will compile? (Choose all that
apply.)
A. List<String> x = new LinkedList<String>();
B. TreeSet<String> x = new TreeSet<String>();
C. HashSet<String> x = new HashSet<String>();
D. Queue<String> x = new PriorityQueue<String>();
E. ArrayList<String> x = new ArrayList<String>();
F. LinkedList<String> x = new LinkedList<String>();
30.
12. TreeSet map = new TreeSet();
13. [Link]("one");
14. [Link]("two");
15. [Link]("three");
16. [Link]("four");
17. [Link]("one");
18. Iterator it = [Link]();
19. while ([Link]() ) {
20. [Link]( [Link]() + " " );
21. }
What is the result?
A. Compilation fails.
B. one two three four
C. four three two one
D. four one three two
E. one two three four one
F. one four three two one
G. An exception is thrown at runtime.
H. The print order is not guaranteed.
Section2: Each question 3 marks
31.
public class M3 {
static int i = -3 + test(0);
static int test(int j) {
return i++ + i + i++ + j++ + j;
}
static {
main(null);
i += i++ + i + i++;
main(null);
}
public static void main(String[] args) {
i += test(i++) + i + test(i++) + i;
[Link](i);
}
}
32.
public class M3 {
static class A{
int i;
A(int i){
this.i = i;
}
A test(A obj) {
A a1 = new A(obj == null? 1 : obj.i);
a1.i += 2;
return a1;
}
}
public static void main(String[] args) {
A a1 = new A(1).test(null);
a1 = [Link](a1).test(a1).test(a1);
A a2 = [Link](a1).test(a1);
a1 = [Link](a1).test(a2).test(a1).test(a2);
a2 = [Link](a1).test(a2).test(a1).test(a2);
[Link](a1.i);
[Link](a2.i);
}
}
33. write all the conditions to be followed to achieve the cloning operation
successfully?
34. Write a program to find out the state of a joining thread?
35. Given some PF(Provident Fund) Account Number, the task is to check if
they are valid or not using regular expressions. Rules for the valid PF
Account Number are :
PF account number is alphanumeric String and forward slaces.
First five characters are reserved for alphabet letters.
Next 17 characters are reserved for digits(0-9).
It allows only special characters whitespaces and forward slaces.
Its length should be less than equal to if:
o It contains forward slaces: 26
o It contains whitespaces: 26.
o It does not contain forward slaces and whitespaces: 22
36. Develop a method to return middle node of a LinkedList
37.
public class M13 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i <= 10; i++) {
[Link](i);
}
//commented place
}
}
Use the Streams and the method reference at the commented place to
get the output as
246810
38.
public class M13 {
public static void main(String[] args) throws IOException{
File f1 = new File("D:\\sept-batch-dev\\core-
java\\app85\\src\\java8");
// commented place
}
}
Use the streams at the commented place to read all the member names
including all sub members till available?
39.
public class MM13 {
public static void main(String[] args) {
String s1 = "ab 123 test aaa a hello";
int start = 0, end = [Link](' ');
String word;
while(end != -1) {
//commented place
}
word = [Link](start);
[Link](word + ":" + [Link]());
}
}
What could be at the commented place to get the following output?
ab:2
123:3
test:4
aaa:3
a:1
hello:5
Note: don’t use trim method.
40. Given a string s, find the first non-repeating character in it and return its
index. If it does not exist, return -1.. Write a program for the above by
using HashMap