Поможем написать учебную работу
Если у вас возникли сложности с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой - мы готовы помочь.
Если у вас возникли сложности с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой - мы готовы помочь.
1.Given the code. What is the result?
public class TrickyNum<X extends Object> {
private X x;
public TrickyNum(X x) {
this.x = x;
}
private double getDouble() {
return x.doubleValue();
}
public static void main(String args[]) {
TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
System.out.print(a.getDouble());
}
}
A) Compilation fails
B) An exception is thrown at runtime
C) "1.0" is printed
D) "1" is printed
2. Given the code. Select correct calls of methods. (Select all that apply)
import java.util.*;
class Empty {
}
class Extended extends Empty {
}
public class TryMe {
public static void doStuff1(List<Empty> list) {
// some code
}
public static void doStuff2(List list) {
// some code
}
public static void doStuff3(List<? extends Empty> list) {
// some code
}
public static void main(String args[]) {
List<Empty> list1 = new LinkedList<Empty>();
List<Extended> list2 = new LinkedList<Extended>();
// more code here
}
}
B) doStuff2(list2);
C) doStuff2(list1);
D) doStuff3(list1);
E) doStuff3(list2);
F) doStuff1(list2);
3. Given the code. What is the result?
import java.util.Collections;
import java.util.LinkedList;
public class TryMe {
public static void main(String args[]) {
LinkedList<String> list = new LinkedList<String>();
list.add("BbB1");
list.add("bBb2");
list.add("bbB3");
list.add("BBb4");
Collections.sort(list);
for (String str : list) {
System.out.print(str + ":");
}
}
}
4. Given the code. What is the result?
public class Cruiser implements Runnable {
public static void main(String[] args) {
Thread a = new Thread(new Cruiser());
a.start();
System.out.print("Begin");
a.join();
System.out.print("End");
}
public void run() {
System.out.print("Run");
}
}
5. Which code, inserted at line labeled "//some code goes here", allows the class Test to be compiled?
class Util {
public enum State{ACTIVE, DELETED, INACTIVE}
}
public class Test {
public static void main(String args[]) {
//some code goes here
}
}
6. What can directly access and change the value of the variable roomNr?
package com.mycompany;
public class Hotel {
public int roomNr = 100;
}
7. Given the code. What is the result?
public static void main(String args[]) {
try {
String arr[] = new String[10];
arr = null;
arr[0] = "one";
System.out.print(arr[0]);
} catch(NullPointerException nex) {
System.out.print("null pointer exception");
} catch(Exception ex) {
System.out.print("exception");
}
}
8. Given the code. What is the result?
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class HashTest {
private String str;
public HashTest(String str) {
this.str = str;
}
public String toString() {
return this.str;
}
public static void main(String args[]) {
HashTest h1 = new HashTest("2");
String s1 = new String("1");
List<Object> list = new LinkedList<Object>();
list.add(h1);
list.add(s1);
Collections.sort(list);
for (Object o : list) {
System.out.print(o + " ");
}
}
}
9. Given the code. What is the result?
public class Hotel {
private static void book() {
System.out.print("book");
}
public static void main(String[] args) throws InterruptedException {
Thread.sleep(1);
book();
}
}
10. Given the code. What is the result?
class Vehicle {
public void printSound() {
System.out.print("vehicle");
}
}
class Car extends Vehicle {
public void printSound() {
System.out.print("car");
}
}
class Bike extends Vehicle {
public void printSound() {
System.out.print("bike");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
11. Given the code. What is true?
public class Room {
public int roomNr;
private Date beginDtm;
private Date endDttm;
public void book(int roomNr, Date beginDttm, Date endDttm) {
this.roomNr = roomNr;
this.beginDtm = beginDttm;
this.endDttm = endDttm;
}
}
12. Given the code. What is the result?
1 public class TryMe {
2 Integer A;
3 int a;
4 public TryMe(int a) {
5 this.a = A + a;
6 System.out.print(this.a);
7 }
8 public static void main(String args[]) {
9 Integer A = new Integer(1);
10 TryMe t = new TryMe(A);
11 }
12 }
13. Given the code. What is the result?
class Small {
public Small() {
System.out.print("a ");
}
}
class Small2 extends Small {
public Small2() {
System.out.print("b ");
}
}
class Small3 extends Small2 {
public Small3() {
System.out.print("c ");
}
}
public class Test {
public static void main(String args[]) {
new Small3();
}
}
14. Given the code. What is the result?
String test = "This is a test string";
String[] tokens = test.split("\\s");
System.out.println(tokens.length);
15. Given the code. What is the result?
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class TryMe {
public static void main(String args[]) {
List list = new LinkedList<String>();
list.add("one");
list.add("two");
list.add("three");
Collections.reverse(list);
Iterator iter = list.iterator();
for (Object o : iter) {
System.out.print(o + " ");
}
}
}
16. Given the code. What is the output?
public class Test {
int a = 10;
public void doStuff(int a) {
a += 1;
System.out.println(++a);
}
public static void main(String args[]) {
Test t = new Test();
t.doStuff(3);
}
}
17. Someone is writing a class MyKey, that will be used as a key in the java.util.HashMap. Which methods should be overriden to guarantee that MyKey works as HashMap key? (Select all that apply)
18. Which three statements concerning the use of the java.io.Serializable interface are true? (Choose three)
19. Given the code. What is the result?
import java.util.HashSet;
public class HashTest {
private String str;
public HashTest(String str) {
this.str = str;
}
public int hashCode() {
return this.str.hashCode();
}
public boolean equals(Object obj) {
return this.str.equals(obj);
}
public static void main(String args[]) {
HashTest h1 = new HashTest("1");
HashTest h2 = new HashTest("1");
String s1 = new String("2");
String s2 = new String("2");
HashSet<Object> hs = new HashSet<Object>();
hs.add(h1);
hs.add(h2);
hs.add(s1);
hs.add(s2);
System.out.print(hs.size());
}
}
20. Given the code. What is the result?
import java.io.*;
public class Hotel implements Serializable {
private transient Room room = new Room();
public static void main(String[] args) {
Hotel h = new Hotel();
try {
FileOutputStream fos = new FileOutputStream("Hotel.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(h);
oos.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
class Room { }
21. Given the code. What is the result?
import java.util.*;
public class TryMe {
public static void main(String args[]) {
Queue<String> q = new PriorityQueue<String>();
q.add("3");
q.add("1");
q.add("2");
System.out.print(q.poll() + " ");
System.out.print(q.peek() + " ");
System.out.print(q.peek());
}
}
22. Given the code. What is the result?
public static void main(String args[]) {
String str = null;
if (str.length() == 0) {
System.out.print("1");
} else if (str == null) {
System.out.print("2");
} else {
System.out.print("3");
}
}
23. What is the result?
public class Hotel {
public static void book(short a) {
System.out.print("short ");
}
public static void book(Short a) {
System.out.print("SHORT ");
}
public static void book(Long a) {
System.out.print("LONG ");
}
public static void main(String[] args) {
short shortRoom = 1;
int intRoom = 2;
book(shortRoom);
book(intRoom);
}
}
24. Given the code. What is the result?
import java.util.Arrays;
public class HashTest {
private String str;
public HashTest(String str) {
this.str = str;
}
public String toString() {
return this.str;
}
public static void main(String args[]) {
HashTest h1 = new HashTest("2");
String s1 = new String("1");
Object arr[] = new Object[2];
arr[0] = h1;
arr[1] = s1;
Arrays.sort(arr);
for (Object o : arr) {
System.out.print(o + " ");
}
}
}
25. Given the code. What is the result when this program is executed?
public class SuperHotel {
static int x[];
static {
x[0] = 1;
}
public static void main(String args[]) {
}
}
26. A Java bean component has the following field:
private boolean done;
Which method declarations follow the JavaBean standards for getting/settings this field? (Choose 3)
27. Given the code. What is true about it? (Choose all that apply)
enum State{ACTIVE, INACTIVE, DELETED}
28. What is true about has-a and is-a relationships? (Choose two)
29. Given the code. What is the result?
public class SomeClass {
private Integer value = 1;
public Integer getValue() {
return value;
}
public void changeVal(Integer value) {
value = new Integer(3);
}
public static void main(String args[]) {
Integer a = new Integer(2);
SomeClass c = new SomeClass();
c.changeVal(a);
System.out.print(a);
}
}
30. Given the code. What is the result?
public class TryMe {
public static void printB(String str) {
System.out.print(Boolean.valueOf(str) ? "true" : "false");
}
public static void main(String args[]) {
printB("tRuE");
printB("false");
}
}
31. Give the code. What is the result?
class Hotel {
public int bookings;
public void book() {
bookings++;
}
}
public class SuperHotel extends Hotel {
public void book() {
bookings--;
}
public void book(int size) {
book();
super.book();
bookings += size;
}
public static void main(String args[]) {
SuperHotel hotel = new SuperHotel();
hotel.book(2);
System.out.print(hotel.bookings);
}
}
32. Given the code. What is the result?
public class Cruiser implements Runnable {
public void run() {
System.out.print("go");
}
public static void main(String arg[]) {
Thread t = new Thread(new Cruiser());
t.run();
t.run();
t.start();
}
}
33. What is true? (Choose three)
34. The code below is executed by "java -Dmyprop=myprop Test". Which two, placed instead of "//some code goes here", will produce the output "myprop"? (Choose two)
public class Test {
public static void main(String args[]) {
String prop = //some code goes here//
System.out.print(prop);
}
}
35. Given the code. What is the output?
1. public static void main(String args[]) {
2. Object myObj = new String[]{"one", "two", "three"};{
3. for (String s : (String[])myObj) System.out.print(s + ".");
4. }
5. }
36. Give a piece of code. What is true?
public void waitForSomething() {
SomeClass o = new SomeClass();
synchronized (o) {
o.wait();
o.notify();
}
}
37.Given the code. What is the result?
public static void main(String args[]) {
String str = null;
if (str == null) {
System.out.print("1");
} else (str.length() == 0) {
System.out.print("2");
} else {
System.out.print("3");
}
}
38. Given the code. What is the result after the class TryMe execution?
class A {
public void doA() {
B b = new B();
b.dobB();
System.out.print("doA");
}
}
class B {
public void dobB() {
C c = new C();
c.doC();
System.out.print("doB");
}
}
class C {
public void doC() {
if (true)
throw new NullPointerException();
System.out.print("doC");
}
}
public class TryMe {
public static void main(String args[]) {
try {
A a = new A();
a.doA();
} catch (Exception ex) {
System.out.print("error");
}
}
}
39. Which three will compile without exception? (Choose three)
40. Given the code. What is the result?
1. public static void main(String args[]) {
2. Object myObj = new String[]{"one", "two", "three"} {
3. for (String s : (String[])myObj) System.out.print(s + ".");
4. }
5. }
41. What do you need to do to correct compilation errors? (Select two)
public class Creature {
private int legCount;
private int wingCount;
public Creature(int legCount) {
this.legCount = this.legCount;
this.wingCount = 0;
}
public String toString() {
return "legs=" + this.legCount + " wings=" + wingCount;
}
}
public class Animal extends Creature {
public Animal(int legCount) {
this.wingCount = 0;
}
}
42. Given the code. Which statements are true? (Select two)
public class Hotel {
public static void book() {
//some code goes here
}
public void cancelBooking() {
//some code goes here
}
}
43. What is true about objects referenced by a, b, aa at the line labeled "// some code goes here"?
class A {
private B b;
public A() {
this.b = new B(this);
}
}
class B {
private A a;
public B(A a) {
this.a = a;
}
}
public class Test {
public static void main(String args[]) {
A aa = new A();
aa = null;
// some code goes here
}
}
44. Give a piece of code. Which two are possible results? (Select two)
public class Cruiser {
private int a = 0;
public void foo() {
Runnable r = new LittleCruiser();
new Thread(r).start();
new Thread(r).start();
}
public static void main(String arg[]) {
Cruiser c = new Cruiser();
c.foo();
}
public class LittleCruiser implements Runnable {
public void run() {
int current = 0;
for (int i = 0; i < 4; i++) {
current = a;
System.out.print(current + ", ");
a = current + 2;
}
}
}
}
45. Given the code. What is the result?
public class Test {
private static void doStuff(String str) {
int var = 4;
if (var == str.length()) {
System.out.print(str.charAt(--var) + " ");
}
else {
System.out.print(str.charAt(0) + " ");
}
}
public static void main(String args[]) {
doStuff("abcd");
doStuff("efg");
doStuff("hi");
}
}
46. When comparing java.io.BufferedWriter to java.io.FileWriter, which capability exist as a method in only one of the two?
A)Closing the stream
B) flushing the stream
C) writing to the stream
D) marking a location in the stream
E) writing a line separator to the stream
47.Given the code. What is the result?
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2009);
c.set(Calendar.MONTH, 6);
c.set(Calendar.DAY_OF_MONTH, 1);
String formattedDate = df.format(c.getTime());
System.out.println(formattedDate);
48.Given:
d is a valid, non-null java.util.Date object
df is a valid, non-null java.text.DateFormat object set to the current locale.
What outputs the current locale's country name and the appropriate version of date?
49.Given the code. What is the result?
public class EmptyStringsTest {
public static boolean isEmpty(String s) {
return (s == null | s.length() == 0);
}
public static void main(String args[]) {
if (isEmpty(null)) {
System.out.print("empty ");
} else {
System.out.print("not_empty ");
}
}
}
50.Given the code. What is the result?
1. int i = 10;
2. while (i++ <= 10) {
3. i++;
4. }
5. System.out.print(i);
51.Given the code below. This code is run by "java Test 1 2 3 4". What is the result?
public class Test {
public static void main(String args[]) {
for (int i = 1; i < args.length; i++) {
System.out.print(i + " ");
}
}
}
52.Given the code. Which line of code marks the earliest point that an object referenced by myInt becomes a candidate for garbage collection?
1 public void doStuff() {
2 Integer arr[] = new Integer[5];
3 for (int i = 0; i < arr.length; i++) {
4 Integer myInt = new Integer(i);
5 arr[i] = myInt;
6 }
7 System.out.println("end");
8 }
53. Given two classes defined in two different files. What is required at line marked "//some code goes here" to process the method doStuff() of a class A?
// The first file
package pack1;
public class ClassA {
public static void doStuff() {
System.out.println("doStuff");
}
}
// The second file
package pack2;
public class ClassB {
public static void main(String args[]) {
//some code goes here
}
}
54. Given the code. What is the result if NullPointerException occurs at line 2?
1. try {
2. //some code goes here
3. }
4. catch (NullPointerException ne) {
5. System.out.print("1 ");
6. }
7. catch (RuntimeException re) {
8. System.out.print("2 ");
9. }
10. finally {
11. System.out.print("3");
12. }
A) 1
B) 3
C) 1 3
D) 2 3
E) 1 2 3
F) 3 1
55. Given the code. What is the result?
class Vehicle {
public void printSound() {
System.out.print("vehicle");
}
}
class Car extends Vehicle {
public void printSound() {
System.out.print("car");
}
}
class Bike extends Vehicle {
public void printSound() {
System.out.print("bike");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
Car c = (Car) v;
v.printSound();
c.printSound();
}
}
56. Given the code. What is the result?
public class Hotel {
private static void book() {
System.out.print("book");
}
public static void main(String[] args) {
Thread.sleep(1);
book();
}
}
57. Given the code. What is the result?
public class TrickyNum<X extends Object> {
private X x;
public TrickyNum(X x) {
this.x = x;
}
private double getDouble() {
return ((Double) x).doubleValue();
}
public static void main(String args[]) {
TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1));
System.out.print(a.getDouble());
}
}
58. Given the code. What is the result?
String test = "This is a test string";
String[] tokens = test.split("\s");
System.out.println(tokens.length);
59. Given the code. What is the result?
import java.io.*;
public class Hotel implements Serializable {
private Room room = new Room();
public static void main(String[] args) {
Hotel h = new Hotel();
try {
FileOutputStream fos = new FileOutputStream("Hotel.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(h);
oos.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
class Room implements Serializable {}
60. Given the code. What is the result?
public static void main(String args[]) {
String str = "null";
if (str == null) {
System.out.print("1");
} else if (str.length() == 0) {
System.out.print("2");
} else {
System.out.print("3");
}
}