Programming Flashcards

1
Q

The programmer, not the compiler, is responsible for testing a program to identify _________________.

A

run-time errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An integrated development environment (IDE) bundles tools for programming into a unified application. What kinds of tools are usually included?

A

an editor and a compiler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do programmers find exceptions and run-time errors?

A

Testing by running the program with a variety of input values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

High-level programming languages ________________________

A

are independent of the underlying hardware

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When a program begins to run, ___________________________.

A

it is moved to the CPU’s memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following typically provides data persistence without electricity?

I. The CPU’s memory
II. The hard disk
III. Secondary storage

A

II and III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

An example of an output device that interfaces between computers and humans is ____________.

A

a speaker

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which one of the following translates high-level descriptions into machine code?

A

compiler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between an editor and a compiler?

A

An editor allows program files to be written and stored; a compiler converts program files into an executable program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Programs that are not running are usually stored _____________________.

A

in secondary storage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Consider the following statements regarding computers.

I. Computers can execute a large number of instructions in a fraction of a second.
II. Computer application areas mainly target the research and scientific communities.
III. The physical components of a computer constitute its hardware.
IV. Unlike humans, a computer never gets bored or exhausted when performing repetitive tasks.

A

I, III, and IV only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A sequence of steps that is unambiguous, executable, and terminating is called _______________.

A

an algorithm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Who or what is responsible for inspecting and testing the program to guard against logic errors?

A

programmer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Computer programming is ______________________.

A

the act of designing and implementing a computer program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Computers are machines that __________________.

A

execute programs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

An example of an input device that interfaces between computers and humans is ____________.

A

a microphone

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Computer scientists have devised __________________ that allow programmers to describe tasks in words that are closer to the syntax of the problems being solved.

A

high-level programming languages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

While developing a program, the programmer adds the discount amount to the total due instead of subtracting it. What type of an error is this?

A

logic error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

The Central Processing Unit is primarily responsible for ______________.

A

performing program control and data processing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Which of the following guidelines will make code more explanatory for others?

A

Add comments to source code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output?

Scanner in = new Scanner(System.in);
System.out.print(“Enter the price: “);
double price = in.nextDouble();

System.out.print(“Enter the discount rate: “);
double discount = in.nextDouble();

System.out.print(“The new price is “);
System.out.println(price - price * (discount / 100.0));

A

The new price is 27.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Assuming that the user inputs “Joe” at the prompt, what is the output of the following code snippet?

public static void main(String[] args)
{
System.out.print(“Enter your name “);
String name;
Scanner in = new Scanner(System.in);
name = in.next();
name += “, Good morning”;
System.out.print(name);
}

A

Joe, Good morning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Which one of the following code snippets compiles without errors and displays the output “Hello Good Day!” on the screen?

A

System.out.print(“Hello “);
System.out.println(“Good Day!”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Assuming that the user inputs a value of 25000 for the pay and 10 for the bonus rate in the following code snippet, what is the output?

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter the pay: “);
double pay = in.nextDouble();
System.out.print(“Enter the bonus rate: “);
double bonus = in.nextDouble();

System.out.println(“The new pay is “ +
(pay + pay * (bonus / 100.0)));
}

A

The new pay is 27500

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the output of the following code snippet?

System.out.print(“Hello”);
System.out.println(“Good Day!”);

A

HelloGood Day!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

At what point in the problem-solving process should one write pseudocode?

A

Before writing Java code, as a guide for a general solution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the output of the following code snippet?

public class PrintIt
{
public static void main(String[] args)
{
System.out.println(“4 * 4” + 12);
}
}

A

1612

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Which of the following statements with comments is(are) valid?

I. int cnt = 0; /* Set count to 0
II. int cnt = 0; /* Set count to 0 */
III. int cnt = 0; // Set count to 0

A

II and III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Assuming that the user inputs a value of 30 for the price and 10 for the discount rate in the following code snippet, what is the output?

Scanner in = new Scanner(System.in);
System.out.print(“Enter the price: “);
double price = in.nextDouble();

System.out.print(“Enter the discount rate: “);
double discount = in.nextDouble();

System.out.print(“The new price is “);
System.out.println(price - price * (discount / 100.0));

A

The new price is 27.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What does the following code do?

int sum = 0;
final int count = 1000;
for (int i = 1; i <= count; i++)
{
sum = sum + (int) (Math.random() * 101);
}
System.out.println(sum / count);

A

It calculates the average of 1000 random integers between 0 and 100.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Which of the following code snippets will generate a random number between 0 and 79?

A

int val = (int) Math.random() * 80;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Assuming that the user enters 23 and 45 as inputs for num1 and num2, respectively, what is the output of the following code snippet?

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter a number: “);
String num1 = in.next();
System.out.print(“Enter another number: “);
String num2 = in.next();
System.out.println(num1 + num2);
}

A

2345

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Which one of the following operators computes the remainder of an integer division?

A

%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the following code snippets simulates that random number?

A

(long) (Math.random() * 1000000 + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What does the following statement sequence print if the user input is 123?

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter a number “);
int myInt = in.nextInt();
myInt += 456;
System.out.println(myInt);
}

A

579

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What does the following code do?

int sum = 0;
final int count = 1000;
for (int i = 1; i <= count; i++)
{
sum = sum + (int) (Math.random() * 101);
}
System.out.println(sum / count);

A

It calculates the average of 1000 random integers between 0 and 100.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Which operator is used to concatenate two or more strings?

A

+

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What is the difference between the result of the following two Java statements?

I. int cents = (int)(100 * price + 0.5);
II. int cents = (100 * price + 0.5);

A

Statement I compiles, but II does not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What is the output of the following code snippet?

public static void main(String[] args)
{
int s;
double f = 365.25;
s = f / 10;
System.out.println(s);
}

A

No output because the code snippet generates compilation errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What does the following statement sequence print?

String str = “Java”;
str += “ is powerful”;
System.out.println(str);

A

Java is powerful

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output?

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter the price: “);
double price = in.nextDouble();
System.out.print(“Enter the discount rate: “);
double discount = in.nextDouble();
System.out.println(“The new price is “ +
price - price * (discount / 100.0));
}

A

The new price is 22.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Imagine you are planning to buy a new cell phone. You are considering two cell phones. These cell phones have different purchase prices. Each mobile service provider charges a different rate for each minute that the cell phone is used. To determine which cell phone is the better buy, you need to develop an algorithm to calculate the total cost of purchasing and using each cell phone. What are all the inputs needed for this algorithm?

A

The cost of each cell phone, the rate per minute for each cell phone, and the number of minutes you would use the cell phone

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Which Java statement prints a blank line?

A

System.out.println();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is the value inside the value variable at the end of the given code snippet?

public static void main(String[] args)
{
int value = 3;
value = value – 2 * value;
value++;
}

A

–2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is the output of the following code snippet?

System.out.print(4 + 4);
System.out.print(12);

A

812

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What does the following statement sequence print if the user input is 123?

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter a number: “);
String str = in.next();
str += 456;
System.out.println(str);
}

A

123456

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities?

A

(int) (Math.random() * 2) + (int) (Math.random() * 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

The Math.ceil method in the Java standard library takes a single value x and returns the smallest integer that is greater than or equal to x. Which of the following is true about Math.ceil(56.75)?

A

The argument is 56.75, and the return value is 57.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Which of the following code snippets can be used for defining a method that does not return a value?
The method should accept a string and then display the string followed by “And that’s all folks!” on a separate line.

A

public void displayMessage(String str)
{
System.out.println(str);
System.out.println(“And that’s all folks!”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

In a vehicle mileage application, you enter number of miles traveled by a vehicle and the amount of fuel used for 30 consecutive days. From this data the average monthly mileage of the vehicle is calculated. Which of the following should be done to improve the program design?

A

Consider writing a method that returns the average monthly mileage as a double value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

An effective technique for understanding the subtle aspects of a method is to _____________.

A

perform a manual walkthrough.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Which of the following code snippets can be used for defining a method that does not return a value?
The method should accept a string and then display the string followed by “Just Let’s learn Java!” on a separate line.

A

public void showString(String someString)
{
System.out.println(someString);
System.out.println(“Just Let’s learn Java!”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Which of the following is the best choice for a return type from a method that prompts users to enter their credit card number exactly as it appears on the card?

A

String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

The term “Black Box” is used with methods because

A

Only the specification matters; the implementation is not important.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

When hand-tracing methods, the values for the parameter variables ______________

A

are determined by the arguments supplied in the code that invokes the method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

What is the output of the following code snippet, assuming the class name is Example?

public void doubleAmount(int intvalue)
{
intvalue = 2 * intvalue;
}
public static void main(String[] args)
{
Example tester = new Example();
int principalAmt = 2000;
System.out.println(tester.doubleAmount(principalAmt));
}

A

Compilation error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

The purpose of a method that returns void is __________________.

A

to package a repeated task as a method even though the task does not yield a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

What is the syntax error in the following method definition?

public String parameter(double r)
{
double result;
result = 2 * 3.14 * r;
return result;
}

A

The value that is returned does not match the specified return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

Which of the following is the correct header for a greaterThan method definition that takes two arguments of type double and returns true if the first value is greater than the second value?

A

public boolean greaterThan(double a, double b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is stepwise refinement?

A

The process of breaking complex problems down into smaller, manageable steps

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

Which of the following is the correct header for a nonstatic method definition named calcSum that accepts four int arguments and returns a double?

A

public double calcSum(int a, int b, int c, int d)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What is the problem with the definition of the following method that calculates and returns the tax due on a purchase amount?

public double taxDue(double amount, double taxRate)
{
double taxDue = 0.0;
taxDue = amount * taxRate;
}

A

The taxDue method does not return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

What is the problem with the code snippet below?
(Assume that a method-calling noun of the class named “tester” exists)

public String val()
{
String result = “candy”;
return;
}
. . .
// Using method val()
System.out.println(“The value is: “ + tester.val());

A

The method val does not have a return value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

Which of the following options describes the process of stepwise refinement?

I. Using arguments to pass information to a method
II. Using unit tests to test the behavior of methods
III. Decomposing complex tasks into simpler ones

A

III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

What can be used as an argument in a method call?

I. A variable

II. An expression

III. Another method call that returns a value

IV. Another method call that has no return value

A

I, II and III only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

What is the syntax error in the following method definition?

public String parameter(double r)
{
double result;
result = 2 * 3.14 * r;
return result;
}

A

The value that is returned does not match the specified return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

You need to write a method that calculates the volume for a shape, which depends on the shape’s length, width, and height. What should be the parameter variables and their data types for this method?

A

double width, double length, double height

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is the problem with the definition of the following method that calculates and returns the tax due on a purchase amount?

public double taxDue(double amount, double taxRate)
{
double taxDue = 0.0;
taxDue = amount * taxRate;
}

A

The taxDue method does not return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Which of the following is NOT a good practice when developing a computer program?

A

Put as many statements as possible into the main method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

For a program that reads city names repeatedly from the user and calculates the distance from a company’s headquarters, which of the following would be a good design based on stepwise refinement?

A

Write one method that reads city name and another method that calculates distance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

If a method is declared to return void, then which statement below is true?

A

When the method terminates no value will be returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

Which of the following is not legal in a method definition?

A

Multiple return values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

What step should you take after implementing a method?

A

Test the method in isolation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is the output of the following code snippet?
Assume that the class is named Example.

public int doIt(int a, int prv1, int nxt1)
{
int prv = a - prv1;
int nxt = a + nxt1;
return prv;
}
public static void main(String[] args)
{
int a = 100;
int b = 100;
int c = 100;
Example tester = new Example();
b = tester.doIt(a, b, c);
System.out.println(“b = “ + b + “, c = “ + c);
}

A

b = 0, c = 100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

Which of the following code snippets can be used for defining a method that does not return a value?
The method should accept a string and then display the string followed by “Just Let’s learn Java!” on a separate line.

A

public void showString(String someString)
{
System.out.println(someString);
System.out.println(“Just Let’s learn Java!”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What is the syntax error in the following method definition?

public String parameter(double r)
{
double result;
result = 2 * 3.14 * r;
return result;
}

A

The value that is returned does not match the specified return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What is the output of the following code snippet?
Assume that the class name is Example.

public int assignPriority(int priority)
{
return priority + 2;
}
public static void main(String[] args)
{
Example tester = new Example();
int priority = tester.assignPriority(3);
System.out.println(“Priority: “ + priority);
}

A

Priority: 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

Given the following method, what is the result of calling the method, assuming a method calling noun of the class named “tester” exists, as tester.getNumber(n)?

public double getNumber(double n)
{
return Math.pow(Math.sqrt(n), 2) - n;
}

A

Close to 0, but not always 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

Parameter variables should not be changed within the body of a method because _______________.

A

it mixes the concept of a parameter with that of a variable

69
Q

When hand-tracing methods, the values for the parameter variables ______________________.

A

are determined by the arguments supplied in the code that invokes the method

70
Q

One advantage of designing methods as black boxes is that

A

many programmers can work on the same project without knowing the internal implementation details of methods.

71
Q

Suppose one needs an if statement to check whether an integer variable pitch is equal to 440 (which is the frequency of the note “A” to which strings and orchestras tune). Which condition is correct?

A

if (pitch == 440

72
Q

Which of the following statements is (are) true about an if statement?

I. It guarantees that several statements are always executed in a specified order.

II. It repeats a set of statements as long as the condition is true.

III. It allows the program to carry out different actions depending on the value of a condition.

A

III only

72
Q

What is the output of the following code snippet?

int num = 100;
if (num != 100)
{
System.out.println(“100”);
}
else
{
System.out.println(“Not 100”);
}

A

Not 100

73
Q

The switch statement in Java ________________.

A

is like a sequence of if statements that compares a value against several constant alternatives

74
Q

What is the output of the following code snippet?

int age = 25;
if (age > 30)
{
System.out.println(“You are wise!”);
}
else
{
System.out.println(“You have much to learn!”);
}

A

You have much to learn!

75
Q

Which of the following statements can be used to validate that the user input for the guess variable is between 1 and 10 inclusive?

A

if (guess >= 1 && guess <= 10)

76
Q

Which of the following variables is used to store a condition that can be either true or false?

A

Boolean

77
Q

Which statement about an if statement is true?

A

The condition in an if statement using relational operators will evaluate to a Boolean result.

77
Q

Which of the following operators is NOT a relational operator?

A

+=

78
Q

What is the conditional required to check whether the length of a string s1 is odd?

A

if ((s1.length() % 2) != 0)

79
Q

Which of the following conditions tests for the user to enter the string “Hello”?

String s;
Scanner in = new Scanner(System.in);
System.out.print(“Enter a word: “);
s = in.next();

A

if (s.equals(“Hello”))

80
Q

When testing code for correctness, it always makes sense to _______________.

A

identify boundary cases and test them

80
Q

What kind of operator is the <= operator?

A

Relational

81
Q

Consider the following code snippet. What is the potential problem with the if statement?

double average;
average = (g1 + g2 + g3 + g4) / 4.0;
if (average == 90.0)
{
System.out.println(“You earned an A in the class!”);
}

A

Using == to test the double variable average for equality is error-prone

82
Q

Which of the following conditions tests whether the user enters the single digit 5?

String s;
Scanner in = new Scanner(System.in);
System.out.print(“Enter a single digit: “);
s = in.next();

A

if (s.equals(“5”))

83
Q

Which of the following options correctly represents a “nested if” structure?
if (tax_rate < 0.10) { . . . }
if (cost < 70) { . . . }

if (cost < 70)
{
if (tax_rate < 0.10) { . . . }
}

if (cost < 70) { . . . }
else { . . . }
if (tax_rate < 0.10) { . . . }

if (cost < 70) { . . . }
if (tax_rate < 0.10) { . . . }

A

if (cost < 70)
{
if (tax_rate < 0.10) { . . . }
}

83
Q

The following code snippet contains an error. What is the error?

if (cost > 100);
{
cost = cost – 10;
}
System.out.println(“Discount cost: “ + cost);

A

Logical error: if statement has do-nothing statement after if condition

84
Q

Assuming that the user provides 303 as input, what is the output of the following code snippet?

int x;
int y;
x = 0;
Scanner in = new Scanner(System.in);
System.out.print(“Please enter a number: “);
y = in.nextInt();
if (y > 300)
{
x = y;
}
else
{
x = 0;
}
System.out.println(“x: “ + x);

A

x: 303

85
Q

An if statement inside another if statement is called a _________________.

A

nested if statement

85
Q

Which of the following statements is correct about an if statement?

A

You can omit an else statement if there is no task defined in the else branch.

85
Q

public class Auto
{
private String make;
private String model;
private String year;
public Auto(String aMake, String aModel, String aYear)
{
make = aMake;
model = aModel;
year = aYear;
}
public String calcMileage(double milesDriven, double gasUsed)
{
double milage = milesDriven/gasUsed;
return mileage.toString();
}
}
Which of the following code snippets will test the creation of an object of this class and display its information?Auto and display its mileage?

A

Auto myAuto = new Auto(“Ford”, “Focus”, “2011”);
System.out.println(myAuto.calcMileage(246.0, 15.8));

86
Q

Consider the following code snippet:

public class BankAccount
{
private int transactionCount;
private double balance;
. . .
}
Which of the following deposit methods would correctly track how many deposits occurred?

A

public void deposit(double amount)
{
balance = balance + amount;
transactionCount ++;
}

87
Q

Each object of a class has a separate copy of each ___.

A

instance variable

87
Q

Which of the following is an accessor method of the CashRegister class used in the textbook?

A

getTotal

88
Q

Which of the following statements about class properties is correct?

A

A setter method is used to assign the value of a class property.

88
Q

Consider the following code snippet:

public class Vehicle
{
private String type;
public String Vehicle(String type)
{
. . .
}
}
What is wrong with this code?

A

The constructor must not have a return type declared.

89
Q

Data required for an object’s use are stored in ____.

A

instance variables

90
Q

You have created a Rocket class which has a constructor with no parameters. Which of the following statements will construct an object of this class?

A

Rocket myRocket = new Rocket();

91
Q

Which of the following is NOT part of the declaration of an instance variable?

A

the return type

92
Q

Complete the following code snippet so that the constructor initializes the class instance variables shown using the parameter values.

public class Employee
{
private String empID;
private boolean hourly;
public Employee(String e, boolean h)
{
_________________
}
}

A

empID = e;
hourly = h;

92
Q

Private instance variables ___.

A

can only be accessed by methods of the same class

93
Q

Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable.

public class Motor
{
private int motorSpeed;
. . .
public Motor(int motorSpeed)
{
________
}
}

A

this.motorSpeed = motorSpeed;

94
Q

Consider the following code snippet:

public class Employee
{
private String empID;
private boolean hourly;
public Employee()
{
}
. . .
}

A

The variable empID will be initialized to null and the variable hourly will be initialized to false

95
Q

Consider the following code snippet:

public int getSalary(String empNum)
{ . . . }
Which of the following statements is correct?

A

empNum is an explicit parameter.

96
Q

The ____ operator is used to construct an object from a class.

A

new

96
Q

A method in a class that returns information about an object but does not change the object is called a/an ____ method.

A

accessor

97
Q

Which of the following statements about classes is correct?

A

All instance variables should be declared as private and most instance methods should be declared as public.

98
Q

You are creating a class named Vessel. Which statement correctly declares a constructor for this class?

A

public Vessel()

99
Q

Consider the following code snippet:

public class Vehicle
{
private String type;
private int numAxles;
public Vehicle(String vehicleType, int VehicleAxles) { . . . }
public Vehicle(String vehicleType) { . . . }
}
What is wrong with this code?

A

There is nothing wrong with this code.

100
Q

Which of the following are considered members of a class?

A

All instance variables and methods.

101
Q

Consider the following statements about modeling objects with position:

I. Objects that move during their lifetime remember their position.

II. The instance variables used to store the position depend on the object being modeled.

III. The behavior of the object must be able to update its position.

A

I, II, and III

101
Q

Insert the missing code in the following code fragment. This fragment is intended to initialize an instance variable.

public class Vehicle
{
int numAxles;
public Vehicle(int axles)
{
numAxles = axles;
}
public Vehicle()
{
_________
}
}
Which of the following lines of code will allow the second constructor to call the first constructor?

A

this(0);

102
Q

You have created a Student class. You wish to have a unique, sequential student number assigned to each new Student object that is created. Which of the following declarations, if added to the Student class, would allow you to determine which number was last assigned when you are creating a new Student object?

A

private static int lastAssignedStudentNum = 1500;

102
Q

Given the following class definition, which of the following are considered part of the class’s public interface?

public class CashRegister
{
public static final double DIME_VALUE = 0.1;
private static int objectCounter;
public void updateDimes(int dimes) {. . .}
private boolean updateCounter(int counter) {. . .}
}

A

DIME_VALUE and updateDimes

103
Q

public class Vehicle
{
private String type;
private int numAxles;
public Vehicle(String vehicleType, int vehicleAxles)
{ . . . }
}
Assuming the following declaration, which statement creates an object of type Vehicle?

Vehicle anAuto;

A

anAuto = new Vehicle(“SUV”, 2);

104
Q

Which of the following statements about encapsulation is correct?

A

To use encapsulation, you must provide a set of public methods in the class and hide the implementation details.

104
Q

You are creating a class named Employee. Which statement correctly declares a constructor for this class?

A

public Employee()

105
Q

Which of the following statements using data values in a class is NOT correct?

A

You can use the argument of any instance method in any other method.

105
Q

Consider the following code snippet:

public int getCoinValue(String coinName)
{ . . . }
Which of the following statements is correct?

A

coinName is an explicit parameter.

106
Q

Consider the following code snippet:

public class Course
{
private int studentCount;
. . .
}
Which of the following methods would correctly keep track of the total number of enrollees in a course?

A

public void addEnrollees(int newEnrollees)
{
studentCount = studentCount + newEnrollees;
}

106
Q

A method in a class that modifies information about an object is called a/an ____ method.

A

mutator

107
Q

You should declare all instance variables as ___.

A

private

107
Q

Which type of method modifies the object on which it is invoked?

A

Mutator method.

108
Q

Insert the missing code in the following code fragment. This fragment is intended to implement a method to get the value stored in an instance variable.

public class Vehicle
{
private String model;
private double rpm;
. . .
_______
{
return model;
}
}

A

public String getModel()

109
Q

Which of the following statements about encapsulation is correct?

A

To use encapsulation, you must provide a set of public methods in the class and hide the implementation details.

110
Q

Which of the following declares a sideLength instance variable for a Square class that stores an integer value?

A

private int sideLength;

111
Q

Consider the following code snippet:

Coin coin1 = new Coin(“dime”, 0.10);
Coin coin2 = coin1;
Which of the following statements is NOT correct?

A

coin1 and coin2 each have their own set of instance variables.

112
Q

You are creating a class named Employee. Which statement correctly declares a constructor for this class?

A

public Employee()

112
Q

Which of the following declares a rpmRating instance variable for a Motor class that stores an integer value?

A

private int rpmRating;

113
Q

Consider the following code snippet:

public class Employee
{
private String empName;
. . .
}
Which of the following statements is correct?

A

The empName variable can be accessed only by methods of the Employee class.

114
Q

Which of the following statements about a class is correct?

A

Private instance variables hide the implementation of a class from the class user.

115
Q

The utility that formats program comments into a set of documents that you can view in a Web browser is called ____.

A

javadoc

115
Q

Input to a method enclosed in parentheses after the method name is known as ____.

A

explicit parameters

116
Q

Complete the following code snippet so that the constructor initializes the class instance variables shown using the parameter values.

public class Vehicle
{
private String type;
private int numAxles;
public Vehicle(String t, int n)
{
_________________
}
}

A

type = t;
numAxles = n;

116
Q

You have created an Employee class. You wish to have a unique, sequential employee number assigned to each new Employee object that is created. Which of the following declarations, if added to the Employee class, would allow you to determine which number was last assigned when you are creating a new Employee object?

A

private static int lastAssignedEmpNum = 500;

117
Q

Consider the following code snippet:

public class AutoRace
{
private int lapCount;
private double totalTime;
. . .
}
Which of the following completeLap methods would correctly track how many laps occurred?

A

public void completeLap(double lapTime)
{
totalTime = totalTime + lapTime;
lapCount ++;
}

117
Q

Which of the following declares a rpmRating instance variable for a Motor class that stores an integer value?

A

private int rpmRating;

118
Q

The public constructors and methods of a class form the public _____ of the class.

A

interface

118
Q

Why is the following method header invalid?

public int[5] meth(int[] arr, int[] num)

A

Methods that return an array cannot specify its size.

118
Q

Which one of the following is the correct header for a method named arrMeth that is called like this:

tester.arrMeth(intArray); // intArray is an integer array of size 3

A

public void arrMeth(int[] ar)

119
Q

In a partially filled array, the number of slots in the array that are not currently used is _______________.

A

the length of the array minus the number of elements currently in the array

120
Q

What statement is true about the following code?

int[] values = {2, 3, 5, 8, 11};
int[] data = values;
data[4] = 13;

A

The variable data references an initialized array.

121
Q

Which statements are true about array references?

I. Assigning an array reference to another creates a second copy of the array.

II. An array reference specifies the location of an array.

III. Two array references can reference the same array.

A

II and III only

122
Q

Consider the following line of code:

int[] somearray = new int[30];
Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?

A

System.out.println(somearray[27]);

123
Q

Which code correctly swaps elements in a non-empty data array for valid positions j and k?

A

int t = data[j];
data[j] = data[k];
data[k] = t;

123
Q

It may be necessary to “grow” an array when reading inputs because ___________________.

A

the number of inputs may not be known in advance

123
Q

What is the output of the following code snippet?

int[] myarray = { 10, 20, 30, 40, 50 };
System.out.print(myarray[2]);
System.out.print(myarray[3]);

A

3040

124
Q

Consider the following code snippet:

String[] data = { “123”, “ghi”, “jkl”, “def”, “%&*” };
Which statement sorts the data array in ascending order?

A

Arrays.sort(data);

125
Q

Identify the correct statement for defining an integer array named numarray of ten elements.

A

int[] numarray = new int[10];

125
Q

What is the purpose of this algorithm, assuming currentSize is the companion variable indicating the actual number of elements in the data array?

if (currentSize < data.length)
{
data[currentSize] = e;
currentSize++;
}

A

Inserting an element e in an unordered partially filled array.

126
Q

Which one of the following statements is true about passing arrays to a method?

A

By default, arrays are passed by the value of the memory reference to a method.

126
Q

Which one of the following statements is correct for the given code snippet?

int[] number = new int[3]; // Line 1
number[3] = 5; // Line 2

A

Line 2 causes a bounds error because 3 is an invalid index number.

127
Q

What is the valid range of index values for an array of size 10?

A

0 to 9

127
Q

Why is the following method header invalid?

public int[5] meth(int[] arr, int[] num)

A

Methods that return an array cannot specify its size.

127
Q

When the order of the elements is unimportant, what is the most efficient way to remove an element from an array?

A

Replace the element to be deleted with the last element in the array.

128
Q

What is the error in this array code fragment?

double[] data;
data[0] = 15.25;

A

The array referenced by data has not been initialized.

129
Q

Suppose you wish to write a method that returns the sum of the elements in the partially filled array myArray. Which is a reasonable method header?

A

public static int sum(int[] values, int currSize)

129
Q

What is the output of the code fragment given below?

int i = 0;
int j = 0;
while (i < 27)
{
i = i + 2;
j++;
}
System.out.println(“j=” + j);

A

j=14

129
Q

Which of the following loop(s) should be used when you need to ask a user to enter one data item and repeat the prompt if the data is invalid?

I. for loop

II. while loop

III. do loop

A

III only

130
Q

Insert a while condition for this algorithm that prompts until a valid value is entered.

boolean valid = false;
int input;
while (__________)
{
System.out.print(“Please enter a number between 1 and 10: “);
input = in.nextInt();
if (0 < input && input <= 10)
{
valid = true;
}
else
{
System.out.println(“Invalid input.”);
}
}

A

!valid

130
Q

What is the output of the following code snippet?

int f1 = 0;
int f2 = 1;
int fRes;
System.out.print(f1 + “ “);
System.out.print(f2 + “ “);
for (int i = 1; i < 10; i++)
{
fRes = f1 + f2;
System.out.print(fRes + “ “);
f1 = f2;
f2 = fRes;
}
System.out.println();

A

0 1 1 2 3 5 8 13 21 34 55

131
Q

Insert a while condition for this algorithm that finds the first occurrence of a dash in the string.

boolean found = false;
int i = 0;
while (__________________)
{
ch = str.charAt(i);
if (ch == ‘-‘)
{
found = true;
}
else
{
i++;
}
}

A

!found && i < str.length()

131
Q

What will be the result of running the following code fragment?

int year = 0;
double rate = 5;
double principal = 10000;
double interest = 0;
while (year < 10)
{
interest = (principal * year * rate) / 100;
System.out.println(“Interest “ + interest);
}

A

The code fragment will continue to display the calculated interest forever because the loop will never end.

132
Q

Which code snippet produces the sum of the first n positive even numbers?

int sum = 0;
for (int i = 0; i < n; i++)
{if (i % 2 == 0)
{sum = sum + i;}}

int sum = 0;
for (int i = 1; i <= n; i++)
{sum = sum + i * 2;}

int sum;
for (int i = 1; i <= n; i++)
{sum = sum + i * 2;}

int sum = 0;
for (int i = 1; i <= n; i++)
{if (i % 2 == 0)
{sum = sum + i;}}

A

int sum = 0;
for (int i = 1; i <= n; i++)
{sum = sum + i * 2;}

133
Q

How many times does the following loop run?

int i = 0;
int j = 1;
do
{
System.out.println(“” + i + “;” + j);
i++;
if (i % 2 == 0)
{
j–;
}
}
while (j >= 1);

A

2 times

133
Q

When hand-tracing the loop in the code snippet below, which variables are important to evaluate?

int i = 10;
int j = 5;
int k = -10;
int sum = 0;
while (i > 0)
{
sum = sum + i + j;
i–;
System.out.println(“Iteration: “ + i);
}

A

The variables i and su

133
Q

What will be the output of the following code snippet?

boolean token = false;
do
{
System.out.println(“Hello”);
}
while (token);

A

“Hello” will be displayed only once.

134
Q

What is the output of the code fragment given below?

int i = 0;
int j = 0;
while (i < 125)
{
i = i + 2;
j++;
}
System.out.println(j);

A

63

134
Q

Which statement about this code snippet is accurate?

int years = 50;
double balance = 10000;
double targetBalance = 20000;
double rate = 3;
for (int i = 1; i <= years; i++)
{
if (balance >= targetBalance)
{
i = years + 1;
}
else
{
double interest = balance * rate / 100;
balance = balance + interest;
}
}

A

The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance.

134
Q

Which of the following for loops is illegal?
for (int i = 0) { }
for (int i = 0; ; ) { }
for (int i = 0, k = 1; ; i++) { }
for ( ; ; )

A

for (int i = 0) { }

135
Q

What is the output of the following code snippet?

int i = 1;
while (i <= 10)
{
System.out.println(“Inside the while loop”);
i = i + 10;
}

A

“Inside the while loop” will be displayed only once.

136
Q

What will be the output of the following code snippet?

boolean token = false;
while (token)
{
System.out.println(“Hello”);
}

A

No output after successful compilation

136
Q

Given the following code snippet, what should we change to have 26 alphabet characters in the string str?

String str = “”;
for (char c = ‘A’; c < ‘Z’; c++)
{
str = str + c;
}

A

c <= ‘Z’

136
Q

What is the output of the code snippet given below?

int i = 0;
while (i != 11)
{
System.out.print(i + “ “);
i = i + 3;
}

A

0 3 6 9 …. (infinite loop)

137
Q

What exception type does the following program throw?

public class Test {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}

A

ArithmeticException

137
Q

An instance of ________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

A

Error

137
Q

What will be the result of running the following code fragment?

int year = 0;
double rate = 5;
double principal = 10000;
double interest = 0;
while (year < 10)
{
interest = (principal * year * rate) / 100;
System.out.println(“Interest “ + interest);
}

A

The code fragment will continue to display the calculated interest forever because the loop will never end.

138
Q

Which class do you use to read data from a text file?

A

Scanner

138
Q

What exception type does the following program throw?

public class Test {
public static void main(String[] args) {
int[] list = new int[5];
System.out.println(list[5]);
}
}

A

ArrayIndexOutOfBoundsException

138
Q

An instance of ________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

A

Exception

139
Q

Which method can be used to read a whole line from the file?

A

nextLine

140
Q

Which class do you use to write data into a text file?

A

PrintWriter

140
Q

Which method can be used to create an input object for file temp.txt?

A

new Scanner(new File(“temp.txt”))

140
Q

An instance of ________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

A

RuntimeException

141
Q

A method must declare to throw ________.

A

checked exceptions