Deck 7: Introduction to Classes and Objects
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/27
Play
Full screen (f)
Deck 7: Introduction to Classes and Objects
1
Which of the following statements is true?
A) Each object (instance) of the class shares the class's instance variables.
B) Most instance-variable declarations are preceded with the keyword public, which is an access modifier.
C) Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared.
D) None of the above is true.
A) Each object (instance) of the class shares the class's instance variables.
B) Most instance-variable declarations are preceded with the keyword public, which is an access modifier.
C) Variables or methods declared with access modifier private are accessible only to methods of the class in which they're declared.
D) None of the above is true.
C
2
Which of the following statements is true?
A) Local variables are automatically initialized.
B) Every instance variable has a default initial value-a value provided by Java when you do not specify the instance variable's initial value.
C) The default value for an instance variable of type String is void.
D) The argument types in the method call must be identical to the types of the corresponding parameters in the method's declaration.
E)
A) Local variables are automatically initialized.
B) Every instance variable has a default initial value-a value provided by Java when you do not specify the instance variable's initial value.
C) The default value for an instance variable of type String is void.
D) The argument types in the method call must be identical to the types of the corresponding parameters in the method's declaration.
E)
B
3
Which of the following statements is false?
A) If a class does not define constructors, the compiler provides a default constructor with no parameters.
B) If you declare a constructor for a class, the compiler will not create a default constructor for that class.
C) The UML models constructors in the third compartment of a class diagram.
D) To distinguish a constructor from a class's operations, the UML places the word "constructor" between double quotes before the constructor's name.
A) If a class does not define constructors, the compiler provides a default constructor with no parameters.
B) If you declare a constructor for a class, the compiler will not create a default constructor for that class.
C) The UML models constructors in the third compartment of a class diagram.
D) To distinguish a constructor from a class's operations, the UML places the word "constructor" between double quotes before the constructor's name.
D
4
Which of the following statements is false?
A) Variables declared in the body of a particular method are local variables and can be used only in that method.
B) A method's parameters are local variables of the method.
C) Every method's body is delimited by left and right braces ({ and }).
D) Keyword null indicates that a method will perform a task but will not return any information.
A) Variables declared in the body of a particular method are local variables and can be used only in that method.
B) A method's parameters are local variables of the method.
C) Every method's body is delimited by left and right braces ({ and }).
D) Keyword null indicates that a method will perform a task but will not return any information.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
5
Declaring instance variables ________ is known as data hiding or information hiding.
A) secure
B) private
C) static
D) masked
A) secure
B) private
C) static
D) masked
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
6
When a method terminates, the values of its local variables are ________.
A) saved
B) copied
C) restored
D) lost
A) saved
B) copied
C) restored
D) lost
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following statements is false?
A) The method's return type specifies the type of data returned to a method's caller.
B) Empty parentheses following a method name indicate that the method does not require any parameters to perform its task.
C) When a method that specifies a return type other than void is called and completes its task, the method must return a result to its calling method
D) Classes often provide public methods to allow the class's clients to set or get private instance variables; the names of these methods must begin with set or get.
A) The method's return type specifies the type of data returned to a method's caller.
B) Empty parentheses following a method name indicate that the method does not require any parameters to perform its task.
C) When a method that specifies a return type other than void is called and completes its task, the method must return a result to its calling method
D) Classes often provide public methods to allow the class's clients to set or get private instance variables; the names of these methods must begin with set or get.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following statements is false?
A) Scanner method next reads characters until any white-space character is encountered, then returns the characters as a String.
B) To call a method of an object, follow the object name with a comma, the method name and a set of parentheses containing the method's arguments.
C) A class instance creation expression begins with keyword new and creates a new object.
D) A constructor is similar to a method but is called implicitly by the new operator to initialize an object's instance variables at the time the object is created.
A) Scanner method next reads characters until any white-space character is encountered, then returns the characters as a String.
B) To call a method of an object, follow the object name with a comma, the method name and a set of parentheses containing the method's arguments.
C) A class instance creation expression begins with keyword new and creates a new object.
D) A constructor is similar to a method but is called implicitly by the new operator to initialize an object's instance variables at the time the object is created.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following statements is false?
A) Most classes you'll use in Java programs must be imported explicitly.
B) There's a special relationship between classes that are compiled in the same directory. By default, such classes are considered to be in the same package-known as the default package.
C) Classes in the same package are implicitly imported into main.
D) An import declaration is not required when one class in a package uses another in the same package.
A) Most classes you'll use in Java programs must be imported explicitly.
B) There's a special relationship between classes that are compiled in the same directory. By default, such classes are considered to be in the same package-known as the default package.
C) Classes in the same package are implicitly imported into main.
D) An import declaration is not required when one class in a package uses another in the same package.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
10
Which of the following statements is false?
A) Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension.
B) Every class declaration contains keyword class followed immediately by the class's name.
C) Class, method and variable names are identifiers.
D) An object has attributes that are implemented as instance variables and carried with it throughout its lifetime.
A) Each class declaration that begins with the access modifier private must be stored in a file that has the same name as the class and ends with the .java filename extension.
B) Every class declaration contains keyword class followed immediately by the class's name.
C) Class, method and variable names are identifiers.
D) An object has attributes that are implemented as instance variables and carried with it throughout its lifetime.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following statements is true?
A) Constructors can specify parameters and return types.
B) Constructors can specify parameters but not return types.
C) Constructors cannot specify parameters but can specify return types.
D) Constructors can specify neither parameters nor return types.
A) Constructors can specify parameters and return types.
B) Constructors can specify parameters but not return types.
C) Constructors cannot specify parameters but can specify return types.
D) Constructors can specify neither parameters nor return types.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following statements is false?
A) The javac command can compile multiple classes at once; simply list the source-code filenames after the command with each filename separated by a comma from the next.
B) If the directory containing the app includes only one app's files, you can compile all of its classes with the command javac *.java.
C) The asterisk (*) in javac *.java indicates that all files in the current directory ending with the filename extension ".java" should be compiled.
D) All of the above are true.
A) The javac command can compile multiple classes at once; simply list the source-code filenames after the command with each filename separated by a comma from the next.
B) If the directory containing the app includes only one app's files, you can compile all of its classes with the command javac *.java.
C) The asterisk (*) in javac *.java indicates that all files in the current directory ending with the filename extension ".java" should be compiled.
D) All of the above are true.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
13
Java requires a ________ call for every object that's created.
A) constructor
B) destructor
C) parameterless
D) parameterized
A) constructor
B) destructor
C) parameterless
D) parameterized
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
14
An import declaration is not required if you always refer to a class with its ________ name, which includes its package name and class name.
A) compile-time
B) default package
C) paired
D) fully qualified name
A) compile-time
B) default package
C) paired
D) fully qualified name
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
15
Which of the following statements is true?
A) The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter value between the parentheses after the operation name.
B) The UML indicates an operation's return type by placing a colon and the return value after the parentheses following the operation name.
C) UML class diagrams do not specify return types for operations that do not return values.
D) Declaring instance variables public is known as data hiding or information hiding.
A) The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter value between the parentheses after the operation name.
B) The UML indicates an operation's return type by placing a colon and the return value after the parentheses following the operation name.
C) UML class diagrams do not specify return types for operations that do not return values.
D) Declaring instance variables public is known as data hiding or information hiding.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
16
Each class you create becomes a new ________ that can be used to declare variables and create objects.
A) package
B) instance
C) library
D) type.
A) package
B) instance
C) library
D) type.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following statements is false?
A) In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top one contains the class's name centered horizontally in boldface. The middle one contains the class's attributes, which correspond to instance variables in Java. The bottom one contains the class's operations, which correspond to methods and constructors in Java.
B) UML represents instance variables as an attribute name, followed by a colon and the type.
C) Private attributes are preceded by the keyword private in the UML.
D) The UML models operations by listing the operation name followed by a set of parentheses. A plus sign (+) in front of the operation name indicates that the operation is a public.
A) In the UML, each class is modeled in a class diagram as a rectangle with three compartments. The top one contains the class's name centered horizontally in boldface. The middle one contains the class's attributes, which correspond to instance variables in Java. The bottom one contains the class's operations, which correspond to methods and constructors in Java.
B) UML represents instance variables as an attribute name, followed by a colon and the type.
C) Private attributes are preceded by the keyword private in the UML.
D) The UML models operations by listing the operation name followed by a set of parentheses. A plus sign (+) in front of the operation name indicates that the operation is a public.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
18
You can declare new classes as needed; this is one reason Java is known as a(n) ________ language.
A) portable
B) incremental
C) extensible
D) virtual
A) portable
B) incremental
C) extensible
D) virtual
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
19
If a class does not define constructors, the compiler provides a default constructor with no parameters, and the class's instance variables are initialized to ________.
A) zero
B) null
C) their default values.
D) false
A) zero
B) null
C) their default values.
D) false
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
20
A static method can ________.
A) call only other static methods of the same class directly
B) manipulate only static fields in the same class directly
C) be called using the class name and a dot (.)
D) All of the above.
A) call only other static methods of the same class directly
B) manipulate only static fields in the same class directly
C) be called using the class name and a dot (.)
D) All of the above.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
21
Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4?
A) values[3] = values[4];
Values[4] = values[3];
B) values[4] = values[3];
Values[3] = values[4];
C) int temp = values[3];
Values[3] = values[4];
Values[4] = temp;
D) int temp = values[3];
Values[3] = values[4];
Values[4] = values[3];
A) values[3] = values[4];
Values[4] = values[3];
B) values[4] = values[3];
Values[3] = values[4];
C) int temp = values[3];
Values[3] = values[4];
Values[4] = temp;
D) int temp = values[3];
Values[3] = values[4];
Values[4] = values[3];
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
22
The format specifier ________ is used to output values of type float or double.
A) %f
B) %d
C) %fd
D) %r
A) %f
B) %d
C) %fd
D) %r
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
23
The format specifier %.2f specifies that two digits of precision should be output ________ in the floating-point number.
A) to the left of the decimal point
B) centered
C) to the right of the decimal point
D) None of the above.
A) to the left of the decimal point
B) centered
C) to the right of the decimal point
D) None of the above.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
24
What kind of application tests a class by creating an object of that class and calling the class's methods?
A) Pseudo application.
B) Debugger.
C) Declarator.
D) Test harness.
A) Pseudo application.
B) Debugger.
C) Declarator.
D) Test harness.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
25
A __________ of a class called myClass is another class whose methods call the methods of myClass.
A) consumer
B) servant
C) caller
D) client
A) consumer
B) servant
C) caller
D) client
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
26
How many Book objects are created by the following statement?
Book[] books = new Book[10];
A) 10
B) 0
C) 5
D) None of the above.
Book[] books = new Book[10];
A) 10
B) 0
C) 5
D) None of the above.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
27
Assume class Book has been declared. Which set of statements creates an array of Books?
A) Book[] books;
Books = new Book[numberElements];
B) Book[] books];
Books = new Book()[numberElements];
C) new Book() books[];
Books = new Book[numberElements];
D) All of the above.
A) Book[] books;
Books = new Book[numberElements];
B) Book[] books];
Books = new Book()[numberElements];
C) new Book() books[];
Books = new Book[numberElements];
D) All of the above.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck