Question: Question 1 3 pts (TCOs 1, 2, and 6) When must a program explicitly use the this reference? When accessing a private variable When accessing
Question 1 3 pts (TCOs 1, 2, and 6) When must a program explicitly use the this reference? When accessing a private variable
When accessing a public variable
When accessing a local variable in a method
When accessing an instance variable in a method that has the same name as a method parameter
Question 2 3 pts (TCOs 1, 2, and 6) Declaring instance variables _____ is known as data hiding, or information hiding. secure
public
private
static
Question 3 3 pts (TCOs 1, 2, and 6) Get methods are also commonly called _____ methods. query
accessor
mutator
static
Question 4 3 pts (TCOs 1, 2, and 6) Which of the following statements is true? @Chapter 3 Each object (instance) of the class shares one copy of the classs instance variables.
Most instance-variable declarations are preceded with the keyword public, which is an access modifier.
Variables or methods declared with access modifier private are accessible only to methods of the class in which they are declared.
Classes often provide public methods to allow the classs clients to set or get private instance variables; the names of these methods must begin with set or get.
Question 5 3 pts (TCOs 1, 2, and 6) Assume a class has been created called Colors. Which statement creates an instance of this class called red? Colors red = new red;
Colors red = new Colors;
Colors red = new red();
Colors red = new Colors();
Question 6 3 pts (TCOs 1, 2, and 6) If a class contains a main method, that method is executed automatically when the class is run.
when another method within the class calls it.
when an object is created from the class.
None of the above
Question 7 3 pts (TCOs 1, 2, and 6) Which statement will print num shown below with two decimal places? double num = 54.321; System.out.printf(2%f, num);
System.out.printf(%.2f, num);
System.out.printf(%2.f, num);
System.out.printf(%.2f, num);
Question 8 3 pts (TCOs 1, 2, and 6) What error, if any, is in the following code? public class Test { private int num; private void setNum(int n) { num = n; } } setNum should have no parameter.
setNum must be public.
setNum should return int, not void.
Code is correct as is.
Question 9 3 pts (TCOs 1, 2, and 6) What error, if any, is in the following code? double num; Scanner input = new Scanner(System.in); System.out.print(Enter a number: ); num = Double.parseDouble(input.nextLine()); The variable num must have an initial value.
The Scanner object must be created before num is declared.
The method nextDouble must be used instead of nextLine.
This code is correct as is.
Question 10 3 pts (TCOs 1, 2, and 6) What does the following code display? public class RentalApp {
public static void main(String[] args) { Rental r = new Rental(); r.setNumOfPersons(5); r.addPerson(); r.addPerson(); System.out.println(r.getNumOfPersons()); } }
public class Rental { private int numOfPersons;
public int getNumOfPersons() { return numOfPersons; }
public void setNumOfPersons(int numOfPersons) { this.numOfPersons = numOfPersons; }
public void addPerson() { numOfPersons = numOfPersons + 1; } } 0
5
6
7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
