Practice Midterm 2 Exam

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which of the following operators compare using short-circuit evaluation?

&&

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?

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

Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities?

(int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1)

What is the first and last value of i to be displayed by the following code snippet? int n = 20; for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { System.out.println("" + i); } }

0 and 20

What is the output from the following Java program? public class Test { public static void main(String[] args) { for (int i = 0; i < 4; i++) { System.out.print(myFun(i) + " "); } System.out.println(); } public static int myFun(int perfect) { perfect = 0; return ((perfect - 1) * (perfect - 1)); } }

1 1 1 1

What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5? double total = 0; boolean hasValidNumber = true; Scanner in = new Scanner(System.in); while (in.hasNextDouble() && hasValidNumber) { double input = in.nextDouble(); if (input < 0) { hasValidNumber = false; } else { total = total + input; } } System.out.println(total);

10.0

What is the output of the following code snippet? int counter = 1; for (double i = 0.01; i <= 1.0; i = i + 0.01) { counter++; } System.out.println(counter);

100

Which of the following statements is valid with respect to the usage of a semicolon in Java?

A semicolon is used to denote the end of a statement.

Suppose that a program asks a user to enter multiple integers, either positive or negative, to do some calculation. The data entry will stop when the user enters a certain value to indicate the end of the data. What value should the code use as the sentinel?

An alphabetical character

What is a logic error?

An error that occurs when a program is running because, for example, the wrong operator was used.

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

Boolean

Imagine you are developing an algorithm to calculate the total cost of a purchase order that contains several items. The cost of each item and the tax rate is known. The standard shipping charge for the entire order is $4.95, and the special delivery charge is $19.95. In addition, there is no tax on the shipping cost. Which of the following is the correct pseudocode for the required algorithm?

For each item on the purchase order: Order cost = order cost + item cost If standard shipping Shipping cost = 4.95 Else Shipping cost = 19.95 Total purchase order cost = order cost + order cost * tax rate + shipping cost

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

III only

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"); }

Not 100

What is one of the benefits of using a high-level programming language like Java?

Problems solved in a high-level language are independent of the underlying computer hardware.

Which of the following is the best choice for a return type from a method that prompts users to enter their email?

String

What step should you take after implementing a method?

Test the method in isolation.

For the given code snippet, which of the following statements is true? public static double raise(double rate) { double newPayRate = rate * 1.1; return newPayRate; } public static void main(String[] args) { double rate = 40.0; double newPayRate = 0.0; newPayRate = raise(rate); System.out.println("Pay rate: " + newPayRate); }

The code snippet executes and displays "Pay rate: 44.0"

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; } }

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

What is the problem with the code snippet below? public class Test { public static void main(String[] args) { System.out.println(cost(10, 4)); } public static void cost(int price, int reps) { for (int i = 0; i < reps; i++) { System.out.print(price); } } }

The method cost returns void and cannot be used as an expression in a print statement.

Assuming that a user enters 15 as input, what is the output of the following code snippet? Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); int number = in.nextInt(); if (number > 20) { System.out.println("The number is LARGE!"); } else { System.out.println("The number is SMALL!"); }

The number is SMALL!

What is the output from this code snippet? System.out.print("The sum is "); System.out.println("7 + 3");

The sum is 7 + 3

Consider a situation where you are buying videos online. The video seller charges $21.50 as the price per video and $6.75 as the handling cost for up to three videos. For every video purchased in addition to three videos, there is a handling charge of $1.50. In addition, there is a 9 percent tax on the cost of the videos but not on the handlingCharges. Assuming that numVideos represents the number of videos you are purchasing, which of the following is the correct algorithm for calculating the total cost of your purchase?

Total charge for videos = 21.50 * numVideos Tax on the videos = total charge for videos * .09 if (numVideos <= 3) then handlingCharges = 6.75 else handlingCharges = 6.75 + 1.50 * (numVideos - 3) Total cost of order = total charge for videos + tax + handlingCharges

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!"); }

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

Class

a blueprint for how a data structure should function

Inheritance

allows one class to use functionality defined in another class

What is the output of the code snippet given below? String s = "abcdefghijkl"; int i = 1; do { if (i > 2) { System.out.print(s.substring (1, i)); } i++; } while (i < 5);

bcbcd

What kind of error is it when your program has a syntax error?

compile-time error

The first step in describing an algorithm in pseudocode is ______________.

determine the inputs and the outputs

Which statement completes this algorithm that finds the minimum value. int input = in.nextInt(); int m = input; while (in.hasNextInt()) { input = nextInt(); _____________________________ { m = input; } }

if (input < m)

Which of the following expressions represents a legal way of checking whether a value assigned to the number variable falls between 50 and 100 inclusive?

if (number >= 50 && number <= 100)

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?

if (pitch == 440)

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();

if (s.equals("Hello"))

Object

instance of a class that stores the state of a class

Constructor

instructs the class to set up the initial state of an object

One advantage of designing methods as black boxes is that

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

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

nested if statement

What are the values of num1 and num2 and result after the execution of mystery(num1, num2)? public static int mystery (int firstNum, int secondNum) { firstNum = firstNum * 2; secondNum = secondNum * 3; return firstNum + secondNum; } public static void main(String[] args) { int num1 = 10; int num2 = 11; int result = mystery(num1, num2); }

num1 = 10, num2 = 11 and result is 53

Assuming that a user enters 22 as the price of an object, which of the following hand-trace tables is valid for the given code snippet? int price = 0; String status = ""; Scanner in = new Scanner(System.in); System.out.print("Please enter object's price: "); price = in.nextInt(); if (price >= 50) { status = "reasonable"; if (price >= 75) { status = "costly"; } } else { status = "inexpensive"; if (price <= 25) { status = "reasonable"; } }

price status 0 "inexpensive" 22 "reasonable"

Suppose you need to write a method that calculates the volume of a 3D rectangular solid. Which of the following is the best choice for the declaration of this method?

public static double volume(double w, double h, double l)

Which of the following code snippets returns the factorial of a given number? (Hint: Factorial of 5 = 5! = 1 * 2 * 3 * 4 * 5 = 120)

public static int factorial(int num) { if (num == 1) { return 1; } return num * factorial(num - 1); }

Method

set of instructions that can be called on an object

Return value

specifies the data type that a method will return after it runs

When hand tracing, drawing a line through the value stored in a variable means that _______________.

the value stored there has changed to something new

Parameter

values that can be specified when creating an object or calling a method

In order to run Java programs on a computer, the computer needs to have software called a(n) __________.

virtual machine

In Java, if you want the compiler to interpret what you type as program instructions, you must __________________.

write correct Java statements separated by the semicolon


Kaugnay na mga set ng pag-aaral

Promotion and Advertising / Chapter 22

View Set

Aggression & Violence Chapter 25

View Set

Strategic Communication 2331 Exam 1

View Set