Question 1
Question
What is the output of the following code?
String s = "University";
s.replace("i", "ABC");
System.out.println(s);
Answer
-
UnABCversity
-
UnABCversABCty
-
UniversABCty
-
University
Question 2
Question
Which of the following is the correct statement to return a string from an array a of characters?
Answer
-
String.toString(a)
-
convertToString(a)
-
new String(a)
-
toString(a)
Question 3
Question
Every instance data field f in the class can be referenced using this.f in a static method the same class.
Question 4
Question
Analyze the following code.
Answer
-
The program has compile errors because the variable radius is not initialized.
-
The program has a compile error because a constant PI is defined inside a method.
-
The program has no compile errors but will get a runtime error because radius is not initialized.
-
The program compiles and runs fine.
Question 5
Question
The following program displays ________.
Answer
-
Java
-
Java and HTML
-
and HTML
-
nothing is displayed
Question 6
Question
Which of the following are properties of a constructor?
Answer
-
A constructor must have the same name as the class.
-
A constructor is called using the new operator.
-
Constructors may be overloaded.
Question 7
Question
Which of the following statements are true?
Answer
-
A default constructor is provided automatically if no constructors are explicitly declared in the class.
-
At least one constructor must always be defined explicitly.
-
Every class has a default constructor.
-
The default constructor is a no-arg constructor.
Question 8
Question
What is the output of the following program?
Answer
-
1234567 1234567
-
1234567 7654321
-
7654321 1234567
-
7654321 7654321
Question 9
Question
Which of the following statements is correct?
Answer
-
Integer.parseInt("12", 2);
-
Integer.parseInt(100);
-
Integer.parseInt("100");
-
Integer.parseInt(100, 16);
-
Integer.parseInt("345", 8);
Question 10
Question
BigInteger and BigDecimal are immutable
Question 11
Question
Assume StringBuilder strBuf is "ABCDEFG", after invoking ________, strBuf contains "AEFG".
Answer
-
strBuf.delete(0, 3)
-
strBuf.delete(1, 3)
-
strBuf.delete(1, 4)
-
strBuf.delete(2, 4)
Question 12
Question
Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true?
Answer
-
dates is null.
-
dates[0] is null.
-
dates = new java.util.Date[5] is fine, which assigns a new array to dates.
-
dates = new Date() is fine, which creates a new Date object and assigns to dates.
Question 13
Question
The method equals, compareTo, charAt, and length are in the ________ class.
Answer
-
String
-
StringBuffer
-
StringBuilder
-
Character
Question 14
Question
Every instance data field f in the class can be referenced using this.f in an instance method the same class.
Question 15
Question
Which of the following is true?
Answer
-
You can add characters into a string buffer.
-
You can delete characters into a string buffer.
-
You can reverse the characters in a string buffer.
-
The capacity of a string buffer can be automatically adjusted.
Question 16
Question
String s = "Welcome to Java";
s.replaceAll("a", "BB");
System.out.println(s);
Answer
-
Welcome to Java
-
Welcome to JBBva
-
Welcome to JBBvBB
-
Welcome to JavBB
Question 17
Question
You can create an instance of the Math class.
Question 18
Question
Which of the following can be placed in the blank line in the following code?
Question 19
Question
Which of the following statements will convert a string s into i of int type?
Answer
-
i = Integer.parseInt(s);
-
i = (new Integer(s)).intValue();
-
i = Integer.valueOf(s).intValue();
-
i = Integer.valueOf(s);
-
i = (int)(Double.parseDouble(s));
Question 20
Question
What is the output of the following code?
Question 21
Question
What is the output of Integer.parseInt("10", 2)?
Answer
-
Invalid statement;
-
10;
-
2;
-
1;
Question 22
Question
Which of the following statements are correct?
Answer
-
new java.math.BigInteger("343");
-
new java.math.BigDecimal("343.445");
-
new java.math.BigInteger(343);
-
new java.math.BigDecimal(343.445);
Question 23
Question
________ returns a string.
Question 24
Question
Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
Question 25
Question
An immutable class cannot have ________.
Answer
-
public data fields
-
private data fields
-
public constructors
-
no-arg constructors
-
static data fields
Question 26
Question
Suppose TestCircle1 and Circle1 in Listing 9.1 are in two separate files named TestCircle1.java and Circle1.java, respectively. What is the outcome of compiling TestCircle.java and then Circle.java?
Answer
-
Only TestCircle1.java compiles.
-
Only Circle1.java compiles.
-
Both compile fine.
-
Neither compiles successfully.
Question 27
Question
Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is ________ in the class.
Question 28
Question
________ returns the last character in a StringBuilder variable named strBuf?
Answer
-
strBuf.charAt(strBuf.length() - 1)
-
strBuf.charAt(strBuf.capacity() - 1)
-
StringBuilder.charAt(strBuf.length() - 1)
-
StringBuilder.charAt(strBuf.capacity() - 1)
Question 29
Question
You can always use the default constructor even though the non-default constructors are defined in the class.
Question 30
Question
All local variables in a method have default values.
Question 31
Question
In JDK 1.5, you may directly assign a primitive data type value to a wrapper object. This is called ________.
Answer
-
auto boxing
-
auto unboxing
-
auto conversion
-
auto casting
Question 32
Question
The replace method in the String class replaces a character in the string. So it does change the content of the string.
Question 33
Question
What is the printout for the third statement in the main method?
Answer
-
j is 0
-
j is 1
-
j is 2
-
j is 3
Question 34
Question
What code may be filled in the blank without causing syntax or runtime errors?
Answer
-
test.date
-
date
-
test.date.toString()
-
date.toString()
Question 35
Question
If the parameter is an object, both formal parameter and actual parameter reference to the same object.
Question 36
Question
All local variables in a method have default values.
Question 37
Question
If the parameter is of a primitive type, both formal parameter and actual parameter reference to the same memory.
Question 38
Question
Which is the advantage of encapsulation?
Answer
-
Only public methods are needed.
-
Making the class final causes no consequential changes to other code.
-
It changes the implementation without changing a class's contract and causes no consequential changes to other code.
-
It changes a class's contract without changing the implementation and causes no consequential changes to other code.
Question 39
Question
An object is an instance of a ________.
Answer
-
program
-
class
-
method
-
data
Question 40
Question
Assume s is " abc ", the method ________ returns a new string "abc".
Answer
-
s.trim(s)
-
trim(s)
-
String.trim(s)
-
s.trim()
Question 41
Question
Which of the following statements are true about an immutable object?
Answer
-
The contents of an immutable object cannot be modified.
-
All properties of an immutable object must be private.
-
All properties of an immutable object must be of primitive types.
-
An object type property in an immutable object must also be immutable.
-
An immutable object contains no mutator methods.
Question 42
Question
Variables that are shared by every instances of a class are ________.
Answer
-
class variables
-
public variables
-
private variables
-
instance variables
Question 43
Question
To get a string from the StringBuffer, you use the toString method.
Question 44
Question
Which of the following is the correct statement to return a string from an array a of characters?
Answer
-
toString(a)
-
new String(a)
-
convertToString(a)
-
String.toString(a)
Question 45
Question
A static data field can be accessed from any method in the same class.
Question 46
Question
Analyze the following code:
Answer
-
The variable t is not initialized and therefore causes errors.
-
The variable t is private and therefore cannot be accessed in the main method.
-
t is non-static and it cannot be referenced in a static context in the main method.
-
The variable x is not initialized and therefore causes errors.
-
The program compiles and runs fine.
Question 47
Question
You can declare variables of the same name in a method even though they are in the same block.
Question 48
Question
Java uses ________ to reference the current object.
Answer
-
this
-
that
-
thisObject
-
null
Question 49
Question
You should add the static keyword in the place of ? in Line ________ in the following code:
Question 50
Question
To create an instance of BigDecimal for 454.45, use ________.
Question 51
Question
What is the output of the following code?
Question 52
Question
Analyze the following code.
Question 53
Question
Which of the following statements are correct?
Answer
-
When creating a Random object, you have to specify the seed or use the default seed.
-
If two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical.
-
The nextInt() method in the Random class returns the next random int value.
-
The nextDouble() method in the Random class returns the next random double value.
Question 54
Question
Java assigns a default value to a data member of a class if the data is not initialized.
Question 55
Question
Analyze the following code:
Answer
-
The program has a compilation error because class A is not a public class.
-
The program has a compilation error because class A does not have a default constructor.
-
The program compiles and runs fine and prints nothing.
-
The program would compile and run if you change A a = new A() to A a = new A("5").
Question 56
Question
Which of the following statements are true?
Answer
-
Use the private modifier to encapsulate data fields.
-
Encapsulating data fields makes the program easy to maintain.
-
Encapsulating data fields makes the program short.
-
Encapsulating data fields helps prevent programming errors.
Question 57
Question
Which of the following statements is preferred to create a string "Welcome to Java"?
Answer
-
String s = "Welcome to Java";
-
String s = new String("Welcome to Java");
-
String s; s = "Welcome to Java";
-
String s; s = new String("Welcome to Java");
Question 58
Question
A static data field can be accessed from any method in the same class.
Question 59
Question
What code may be filled in the blank without causing syntax or runtime errors?
Answer
-
test.date
-
date
-
test.date.toString()
-
date.toString()
Question 60
Question
What is displayed by the following code?
Answer
-
A,B;C;D
-
A B C D
-
A B C;D
-
A B;C;D
Question 61
Question
To add BigInteger b1 to b2, you write ________.
Answer
-
b1.add(b2);
-
b2.add(b1);
-
b2 = b1.add(b2);
-
b2 = b2.add(b1);
-
b1 = b2.add(b1);
Question 62
Question
Assume s is "ABCABC", the method ________ returns a new string "aBCaBC".
Question 63
Question
You can declare variables of the same name in a method if they are in non-nesting blocks.
Question 64
Question
Analyze the following code.
Answer
-
The program has a compile error because test is not initialized.
-
The program has a compile error because x has not been initialized.
-
The program has a compile error because you cannot create an object from the class that defines the object.
-
The program has a compile error because Test does not have a default constructor.
-
The program has a runtime NullPointerException because test is null while executing test.x.
Question 65
Question
What is the output of the following code?
Question 66
Question
An immutable class cannot have ________.
Answer
-
public data fields
-
private data fields
-
public constructors
-
no-arg constructors
-
static data fields
Question 67
Question
Assume StringBuilder strBuf is "ABCCEFC", after invoking ________, strBuf contains "ABTTEFT".
Answer
-
strBuf.replace('C', 'T')
-
strBuf.replace("C", "T")
-
strBuf.replace("CC", "TT")
-
strBuf.replace('C', "TT")
-
strBuf.replace(2, 7, "TTEFT")
Question 68
Question
Assume StringBuilder strBuf is "ABCDEFG", after invoking ________, strBuf contains "ABCRRRRDEFG".
Answer
-
strBuf.insert(1, "RRRR")
-
strBuf.insert(2, "RRRR")
-
strBuf.insert(3, "RRRR")
-
strBuf.insert(4, "RRRR")
Question 69
Question
Given the declaration Circle x = new Circle(), which of the following statement is most accurate?
Answer
-
x contains an int value.
-
x contains an object of the Circle type.
-
x contains a reference to a Circle object.
-
You can assign an int value to x.
Question 70
Question
Each class in the file is compiled into a separate bytecode file.
Question 71
Question
Which of the following statements are true?
Answer
-
Local variables do not have default values.
-
Data fields have default values.
-
A variable of a primitive type holds a value of the primitive type.
-
A variable of a reference type holds a reference to where an object is stored in the memory.
-
You may assign an int value to a reference variable.