Quiz
chapters 9 and 10 pt 1
Create a free account to finish this quiz
Save your progress and see your final score.
Already have an account? Sign in
Questions in this quiz (71)
1 What is the output of the following code?
String s = "University";
s.replace("i", "ABC");
System.out.println(s);- AUnABCversity
- BUnABCversABCty
- CUniversABCty
- DUniversity
2Which of the following is the correct statement to return a string from an array a of characters?
- AString.toString(a)
- BconvertToString(a)
- Cnew String(a)
- DtoString(a)
3Every instance data field f in the class can be referenced using this.f in a static method the same class.
- ATrue
- BFalse
4Analyze the following code.

- AThe program has compile errors because the variable radius is not initialized.
- BThe program has a compile error because a constant PI is defined inside a method.
- CThe program has no compile errors but will get a runtime error because radius is not initialized.
- DThe program compiles and runs fine.
5The following program displays ________.

- AJava
- BJava and HTML
- Cand HTML
- Dnothing is displayed
6Which of the following are properties of a constructor?
- AA constructor must have the same name as the class.
- BA constructor is called using the new operator.
- CConstructors may be overloaded.
7Which of the following statements are true?
- AA default constructor is provided automatically if no constructors are explicitly declared in the class.
- BAt least one constructor must always be defined explicitly.
- CEvery class has a default constructor.
- DThe default constructor is a no-arg constructor.
8What is the output of the following program?

- A1234567 1234567
- B1234567 7654321
- C7654321 1234567
- D7654321 7654321
9Which of the following statements is correct?
- AInteger.parseInt("12", 2);
- BInteger.parseInt(100);
- CInteger.parseInt("100");
- DInteger.parseInt(100, 16);
- EInteger.parseInt("345", 8);
10BigInteger and BigDecimal are immutable
- ATrue
- BFalse
11Assume StringBuilder strBuf is "ABCDEFG", after invoking ________, strBuf contains "AEFG".
- AstrBuf.delete(0, 3)
- BstrBuf.delete(1, 3)
- CstrBuf.delete(1, 4)
- DstrBuf.delete(2, 4)
12Assume java.util.Date[] dates = new java.util.Date[10], which of the following statements are true?
- Adates is null.
- Bdates[0] is null.
- Cdates = new java.util.Date[5] is fine, which assigns a new array to dates.
- Ddates = new Date() is fine, which creates a new Date object and assigns to dates.
13The method equals, compareTo, charAt, and length are in the ________ class.
- AString
- BStringBuffer
- CStringBuilder
- DCharacter
14Every instance data field f in the class can be referenced using this.f in an instance method the same class.
- ATrue
- BFalse
15Which of the following is true?
- AYou can add characters into a string buffer.
- BYou can delete characters into a string buffer.
- CYou can reverse the characters in a string buffer.
- DThe capacity of a string buffer can be automatically adjusted.
16String s = "Welcome to Java";
s.replaceAll("a", "BB");
System.out.println(s);- AWelcome to Java
- BWelcome to JBBva
- CWelcome to JBBvBB
- DWelcome to JavBB
17You can create an instance of the Math class.
- ATrue
- BFalse
18Which of the following can be placed in the blank line in the following code?

- Athis
- BTest
19Which of the following statements will convert a string s into i of int type?
- Ai = Integer.parseInt(s);
- Bi = (new Integer(s)).intValue();
- Ci = Integer.valueOf(s).intValue();
- Di = Integer.valueOf(s);
- Ei = (int)(Double.parseDouble(s));
20What is the output of the following code?

- As1 and s2 reference to the same String object
- Bs1 and s2 reference to different String objects
21What is the output of Integer.parseInt("10", 2)?
- AInvalid statement;
- B10;
- C2;
- D1;
22Which of the following statements are correct?
- Anew java.math.BigInteger("343");
- Bnew java.math.BigDecimal("343.445");
- Cnew java.math.BigInteger(343);
- Dnew java.math.BigDecimal(343.445);
23________ returns a string.
- AString.valueOf(123)
- BString.valueOf(12.53)
- CString.valueOf(false)
- DString.valueOf(new char[]{'a', 'b', 'c'})
24Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
- AString s = new String("new string");
- BString s3 = s1 + s2
- Cs1 >= s2
- Dint i = s1.length
- Es1.charAt(0) = '5'
25An immutable class cannot have ________.
- Apublic data fields
- Bprivate data fields
- Cpublic constructors
- Dno-arg constructors
- Estatic data fields
26Suppose 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?
- AOnly TestCircle1.java compiles.
- BOnly Circle1.java compiles.
- CBoth compile fine.
- DNeither compiles successfully.
27Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is ________ in the class.

- Aa static method
- Ban instance method
- Ca static method or an instance method
28________ returns the last character in a StringBuilder variable named strBuf?
- AstrBuf.charAt(strBuf.length() - 1)
- BstrBuf.charAt(strBuf.capacity() - 1)
- CStringBuilder.charAt(strBuf.length() - 1)
- DStringBuilder.charAt(strBuf.capacity() - 1)
29You can always use the default constructor even though the non-default constructors are defined in the class.
- ATrue
- BFalse
30All local variables in a method have default values.
- ATrue
- BFalse
31In JDK 1.5, you may directly assign a primitive data type value to a wrapper object. This is called ________.
- Aauto boxing
- Bauto unboxing
- Cauto conversion
- Dauto casting
32The replace method in the String class replaces a character in the string. So it does change the content of the string.
- ATrue
- BFalse
33What is the printout for the third statement in the main method?

- Aj is 0
- Bj is 1
- Cj is 2
- Dj is 3
34What code may be filled in the blank without causing syntax or runtime errors?

- Atest.date
- Bdate
- Ctest.date.toString()
- Ddate.toString()
35If the parameter is an object, both formal parameter and actual parameter reference to the same object.
- ATrue
- BFalse
36All local variables in a method have default values.
- ATrue
- BFalse
37If the parameter is of a primitive type, both formal parameter and actual parameter reference to the same memory.
- ATrue
- BFalse
38Which is the advantage of encapsulation?
- AOnly public methods are needed.
- BMaking the class final causes no consequential changes to other code.
- CIt changes the implementation without changing a class's contract and causes no consequential changes to other code.
- DIt changes a class's contract without changing the implementation and causes no consequential changes to other code.
39An object is an instance of a ________.
- Aprogram
- Bclass
- Cmethod
- Ddata
40Assume s is " abc ", the method ________ returns a new string "abc".
- As.trim(s)
- Btrim(s)
- CString.trim(s)
- Ds.trim()
41Which of the following statements are true about an immutable object?
- AThe contents of an immutable object cannot be modified.
- BAll properties of an immutable object must be private.
- CAll properties of an immutable object must be of primitive types.
- DAn object type property in an immutable object must also be immutable.
- EAn immutable object contains no mutator methods.
42Variables that are shared by every instances of a class are ________.
- Aclass variables
- Bpublic variables
- Cprivate variables
- Dinstance variables
43To get a string from the StringBuffer, you use the toString method.
- ATrue
- BFalse
44Which of the following is the correct statement to return a string from an array a of characters?
- AtoString(a)
- Bnew String(a)
- CconvertToString(a)
- DString.toString(a)
45A static data field can be accessed from any method in the same class.
- ATrue
- BFalse
46Analyze the following code:

- AThe variable t is not initialized and therefore causes errors.
- BThe variable t is private and therefore cannot be accessed in the main method.
- Ct is non-static and it cannot be referenced in a static context in the main method.
- DThe variable x is not initialized and therefore causes errors.
- EThe program compiles and runs fine.
47You can declare variables of the same name in a method even though they are in the same block.
- ATrue
- BFalse
48Java uses ________ to reference the current object.
- Athis
- Bthat
- CthisObject
- Dnull
49You should add the static keyword in the place of ? in Line ________ in the following code:

- Ain line 4
- Bin line 8
- Cin both line 4 and line 8
- Dnone
50To create an instance of BigDecimal for 454.45, use ________.
- ABigInteger(454.45);
- Bnew BigInteger(454.45);
- CBigInteger("454.45");
- Dnew BigDecimal("454.45");
51What is the output of the following code?

- As1 and s2 reference to the same String object
- Bs1 and s2 reference to different String objects
52Analyze the following code.

- AThe code has a compile error because xMethod does not return a value.
- BThe code has a compile error because xMethod is not declared static.
- CThe code prints n is 1.
- DThe code prints n is 2.
- EThe code prints n is 3.
53Which of the following statements are correct?
- AWhen creating a Random object, you have to specify the seed or use the default seed.
- BIf two Random objects have the same seed, the sequence of the random numbers obtained from these two objects are identical.
- CThe nextInt() method in the Random class returns the next random int value.
- DThe nextDouble() method in the Random class returns the next random double value.
54Java assigns a default value to a data member of a class if the data is not initialized.
- ATrue
- BFalse
55Analyze the following code:

- AThe program has a compilation error because class A is not a public class.
- BThe program has a compilation error because class A does not have a default constructor.
- CThe program compiles and runs fine and prints nothing.
- DThe program would compile and run if you change A a = new A() to A a = new A("5").
56Which of the following statements are true?
- AUse the private modifier to encapsulate data fields.
- BEncapsulating data fields makes the program easy to maintain.
- CEncapsulating data fields makes the program short.
- DEncapsulating data fields helps prevent programming errors.
57Which of the following statements is preferred to create a string "Welcome to Java"?
- AString s = "Welcome to Java";
- BString s = new String("Welcome to Java");
- CString s; s = "Welcome to Java";
- DString s; s = new String("Welcome to Java");
58A static data field can be accessed from any method in the same class.
- ATrue
- BFalse
59What code may be filled in the blank without causing syntax or runtime errors?

- Atest.date
- Bdate
- Ctest.date.toString()
- Ddate.toString()
60What is displayed by the following code?

- AA,B;C;D
- BA B C D
- CA B C;D
- DA B;C;D
61To add BigInteger b1 to b2, you write ________.
- Ab1.add(b2);
- Bb2.add(b1);
- Cb2 = b1.add(b2);
- Db2 = b2.add(b1);
- Eb1 = b2.add(b1);
62Assume s is "ABCABC", the method ________ returns a new string "aBCaBC".
- As.toLowerCase(s)
- Bs.toLowerCase()
- Cs.replace('A', 'a')
- Ds.replace('a', 'A')
- Es.replace("ABCABC", "aBCaBC")
63You can declare variables of the same name in a method if they are in non-nesting blocks.
- ATrue
- BFalse
64Analyze the following code.

- AThe program has a compile error because test is not initialized.
- BThe program has a compile error because x has not been initialized.
- CThe program has a compile error because you cannot create an object from the class that defines the object.
- DThe program has a compile error because Test does not have a default constructor.
- EThe program has a runtime NullPointerException because test is null while executing test.x.
65What is the output of the following code?

- A3
- B4
- C10
- D11
66An immutable class cannot have ________.
- Apublic data fields
- Bprivate data fields
- Cpublic constructors
- Dno-arg constructors
- Estatic data fields
67Assume StringBuilder strBuf is "ABCCEFC", after invoking ________, strBuf contains "ABTTEFT".
- AstrBuf.replace('C', 'T')
- BstrBuf.replace("C", "T")
- CstrBuf.replace("CC", "TT")
- DstrBuf.replace('C', "TT")
- EstrBuf.replace(2, 7, "TTEFT")
68Assume StringBuilder strBuf is "ABCDEFG", after invoking ________, strBuf contains "ABCRRRRDEFG".
- AstrBuf.insert(1, "RRRR")
- BstrBuf.insert(2, "RRRR")
- CstrBuf.insert(3, "RRRR")
- DstrBuf.insert(4, "RRRR")
69Given the declaration Circle x = new Circle(), which of the following statement is most accurate?
- Ax contains an int value.
- Bx contains an object of the Circle type.
- Cx contains a reference to a Circle object.
- DYou can assign an int value to x.
70Each class in the file is compiled into a separate bytecode file.
- ATrue
- BFalse
71Which of the following statements are true?
- ALocal variables do not have default values.
- BData fields have default values.
- CA variable of a primitive type holds a value of the primitive type.
- DA variable of a reference type holds a reference to where an object is stored in the memory.
- EYou may assign an int value to a reference variable.
Information
- Description
- chapters 9 and 10
- Author
- Joe Delisle
- Language
- English
- Views
- 238
- Tags
- Rating
- No ratings yet Rate this quiz