Friday, October 26, 2007

Java interview questions Part30


Given:

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[o]);
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?

234

334

2334

0123456

01234456

12334567

Compilation fails.


2.
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 cl = 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?

pc

pcc

pcp

pcpc

Compilation fails.

An exception is thrown at runtime.


3.
Given that 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?

Compilation succeeds.

Compilation fails due only to an error on line 15.

Compilation fails due only to an error on line 16.

Compilation fails due only to an error on line 17.

Compilation fails due only to an error on line 18.

Compilation fails due to errors on multiple lines.


4.
Given:

Class TKO {
public static void main(String[] args) {
String s = "-";
Integer x = 343;
long L343 = 343L;
if(x.equals(L343)) s += ".e1 ";
if(x.equals(343)) s += ".e2 ";
Short s1 = (short)((new Short((short)343)) / (new Short((short)49)));
if (s1 == 7) s += "=s ";
if(sl < new Integer(7+1)) s += "fly ";
System.out.println(s);
}
}

Which of the following will be included in the output String s? (Choose all that apply.)

.e1

.e2

=s

fly

None of the above.

Compilation fails.

An exception is thrown at runtime.


5.
Given:

1. import java.text.*;
2. class DateOne {
3. public static void main(String[] args) {
4. Date d = new Date(1123631685981L);
5. DateFormat df = new DateFormat();
6. System.out.println(df.format(d));
7. }
8. }

And given that 1123631685981L is the number of milliseconds between Jan. 1, 1970, and sometime on Aug. 9, 2005, what is the result? (Note: the time of day in option A may vary.)

8/9/05 5:54 PM

1123631685981L,

An exception is thrown at runtime.

Compilation fails due to a single error in the code.

Compilation fails due to multiple errors in the code.


6.
Given:

import java.io.*;

class Keyboard { }
public class Computer implements Serializable {
private Keyboard k = new Keyboard();
public static void main(String[] args)
Computer c = new Computer();
c.storeIt(c) ;
}
void storeIt(Computer c) {
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("myFile"));
os.writeObject(c);
os.close() ;
System.out.println("done");
} catch (Exception x) {System.out.println("exc"); } }
}
}

What is the result? (Choose all that apply.)

exc

done

Compilation fails.

Exactly one object is serialized.

Exactly two objects are serialized

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com