chapters 9 and 10 pt 1

Description

chapters 9 and 10
Joe Delisle
Quiz by Joe Delisle, updated more than 1 year ago
Joe Delisle
Created by Joe Delisle almost 7 years ago
222
0

Resource summary

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.
Answer
  • True
  • False

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
Answer
  • True
  • False

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.
Answer
  • True
  • False

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.
Answer
  • True
  • False

Question 18

Question
Which of the following can be placed in the blank line in the following code?
Answer
  • this
  • Test

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?
Answer
  • s1 and s2 reference to the same String object
  • s1 and s2 reference to different String objects

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.
Answer
  • String.valueOf(123)
  • String.valueOf(12.53)
  • String.valueOf(false)
  • String.valueOf(new char[]{'a', 'b', 'c'})

Question 24

Question
Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
Answer
  • String s = new String("new string");
  • String s3 = s1 + s2
  • s1 >= s2
  • int i = s1.length
  • s1.charAt(0) = '5'

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.
Answer
  • a static method
  • an instance method
  • a static method or an instance method

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.
Answer
  • True
  • False

Question 30

Question
All local variables in a method have default values.
Answer
  • True
  • False

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.
Answer
  • True
  • False

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.
Answer
  • True
  • False

Question 36

Question
All local variables in a method have default values.
Answer
  • True
  • False

Question 37

Question
If the parameter is of a primitive type, both formal parameter and actual parameter reference to the same memory.
Answer
  • True
  • False

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.
Answer
  • True
  • False

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.
Answer
  • True
  • False

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.
Answer
  • True
  • False

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:
Answer
  • in line 4
  • in line 8
  • in both line 4 and line 8
  • none

Question 50

Question
To create an instance of BigDecimal for 454.45, use ________.
Answer
  • BigInteger(454.45);
  • new BigInteger(454.45);
  • BigInteger("454.45");
  • new BigDecimal("454.45");

Question 51

Question
What is the output of the following code?
Answer
  • s1 and s2 reference to the same String object
  • s1 and s2 reference to different String objects

Question 52

Question
Analyze the following code.
Answer
  • The code has a compile error because xMethod does not return a value.
  • The code has a compile error because xMethod is not declared static.
  • The code prints n is 1.
  • The code prints n is 2.
  • The code prints n is 3.

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.
Answer
  • True
  • False

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.
Answer
  • True
  • False

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".
Answer
  • s.toLowerCase(s)
  • s.toLowerCase()
  • s.replace('A', 'a')
  • s.replace('a', 'A')
  • s.replace("ABCABC", "aBCaBC")

Question 63

Question
You can declare variables of the same name in a method if they are in non-nesting blocks.
Answer
  • True
  • False

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?
Answer
  • 3
  • 4
  • 10
  • 11

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.
Answer
  • True
  • False

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.
Show full summary Hide full summary

Similar

Computing Hardware - CPU and Memory
ollietablet123
SFDC App Builder 2
Parker Webb-Mitchell
Data Types
Jacob Sedore
Intake7 BIM L1
Stanley Chia
Software Processes
Nurul Aiman Abdu
Design Patterns
Erica Solum
CCNA Answers – CCNA Exam
Abdul Demir
Abstraction
Shannon Anderson-Rush
Spyware
Sam2
HTTPS explained with Carrier Pigeons
Shannon Anderson-Rush
Data Analytics
anelvr