CSCI-Final

Ace your homework & exams now with Quizwiz!

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

"Inside the while loop" will be displayed only once.

Which condition, when supplied in the if statement below in place of (. . .), will correctly protect against division by zero? if (. . .) { result = grade / num; System.out.println("Just avoided division by zero!"); }

(num != 0)

What is the output of the code snippet given below? int i = 0; while (i != 9) { System.out.println("" + i); i = i + 2; }

0 2 4 6 8 10 12....(infinite loop)

What output does this for loop generate? for (int j = 0; j < 5; j++) { int value = j * 2; System.out.print(value + ", ");

0, 2, 4, 6, 8,

Consider the following pseudocode, what does it produce? Set n = 1. Set p = 1. Repeat until n equals 6 Multiply p by n and store result in p. Add 1 to n. Print p.

1 2 6 24 120 720

How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; }

10

What is the value of num after you run the following code snippet? int num = 100; if (num <= 100) { num++; } if (num <= 200) { num--; } if (num <= 300) { num++; } if (num <= 400) { num--; } if (num <= 500) { num++; }

101

What is the output of the following code snippet? int num = 100; if (num < 100) { if (num < 50) { num = num - 5; } else { num = num - 10; } } else { if (num > 150) { num = num + 5; } else { num = num + 10; } } System.out.println(num);

110

Assuming that variable base has a value of 5 and variable height has a value of 12, what output is generated by the code segment below? if (base >= height / 2) { System.out.print(base); } else { System.out.print(height); } System.out.print(" " + (base * height) / 2);

12 30

How many times does the code snippet below display "Hello"? int i = 0; while (i != 15) { System.out.println("Hello"); i++; }

15 times

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

2 times

What output does this do loop generate? int j = 1; do { int value = j * 2; j++; System.out.print(value + ", "); } while (j <= 5);

2, 4, 6, 8, 10,

What is the output of the following code snippet if the input is 25? int i = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); i = in.nextInt(); if (i > 25) { i++; } else { i--; } System.out.print(i);

24

Assuming that a user enters 25 as the value for x, what is the output of the following code snippet? int x = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); x = in.nextInt(); if (x < 100) { x = x + 5; } if (x < 500) { x = x - 2; } if (x > 10) { x++; } else { x--; } System.out.println(x);

29

What is the result when the following code is run? double x = 1; double y = 1; int i = 0; do { y = x / 2; x = x + y; i = i + 1; } while (x < 2.5); System.out.print(i + " ");

3

What output does this while loop generate? int number = 1; while (number <= 5) { int value = number * 3; System.out.print(value + ", "); number++; }

3, 6, 9, 12, 15,

What is the value inside the var variable at the end of the given code snippet? public static void main(String[] args) { int var = 30; var = var + 2 / var; var++; }

31

What is the value of variable result after executing the statements below? int limit = 2; double result = 3.5 + 5 / (6 - limit);

4.5

Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? public static void main(String[] args) { System.out.print("Enter a number: "); Scanner in = new Scanner(System.in); String n1 = in.next(); System.out.print("Enter another number: "); String n2 = in.next(); String result = n1 + n2; System.out.print(result); }

4562

What is the value of variable count after the code segment below is executed? int count = 3; count--; count = count + 2; count++;

5

What is the value of variable count after the statement below is executed? int count = (int) (2.5 * Math.sqrt(9.0) - 2);

5

What is the result of the following expression? double d = 2.5 + 4 * -1.5 - (2.5 + 4) * -1.5;

6.25

Evaluate the pseudocode below to calculate the payment (pmt) with the following test values: The total number of hours worked (workingHours) = 50 The rate paid for hourly work (rate) = 10 Input workingHours. Input rate. pmt = workingHours * rate If working_hours > 45 extraHours = workingHours - 45 extraPmt = extraHours * rate * 2 pmt = pmt + extraPmt Output pmt

600

What is the output of the following code snippet? double a = 2; int n = 16; double r = 1; double b = a; int i = n; while (i > 0) { if (i % 2 == 0) // n is even { b = b * b; i = i / 2; } else { r = r * b; i--; } } System.out.println("r = " + r);

65536.0

What is the value of variable count after the statement below is executed? int count = 3 * 2 + 6 % 5;

7

What is the output of the following code snippet? System.out.print(4 + 4); System.out.print(12);

812

How many times does the code snippet given below display "Loop Execution"? int i = 1; while (i != 10) { System.out.println ("Loop Execution"); i++; }

9 times

Assuming that variable number has a value of 777, what output is generated by the code segment below? if (number > 1000) { if (number % 2 == 0) { System.out.print("A large even number"); } else { System.out.print("A large odd number"); } } else { if (number % 2 == 0) { System.out.print("A small even number"); } else { System.out.print("A small odd number"); } }

A small odd number

What is the difference between an editor and a compiler?

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

What output is generated by the statements below? System.out.print("Easy as"); System.out.print(" 1 + 2 + 3");

Easy as 1 + 2 + 3

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 * tax rate + shipping cost

What is the output of the following code snippet? System.out.print("Hello"); System.out.println("Good Day!");

HelloGood Day!

Assuming that the user enters 60 as the input, what is the output after running the following code snippet? int num = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num = in.nextInt(); if (num < 10) { System.out.println("Too small!"); } else if (num < 50) { System.out.println("Intermediate!"); } else if (num < 100) { System.out.println("High!"); } else { System.out.println("Too high!"); }

High!

What does the following statement sequence print? String str = "Harry"; int n = str.length(); String mystery = str.substring(0, 1) + str.substring(n - 2, n); System.out.println(mystery);

Hry

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

III only

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

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

When a program begins to run,

It is moved to the CPU's memory.

Assuming that variable temperature has a value of 88, what output is generated by the code segment below? if (temperature > 90) { System.out.print("It is"); } else { System.out.print("It is not"); } System.out.print(" a hot day.");

It is not a hot day.

What output is generated by the statements below? System.out.print("Java"); System.out.println(" is "); System.out.print("fun");

Java is fun

In Java, which of the following orderings is used to compare strings?

Lexicographic

What kind of error is created by the following code snippet? System.out.print("The sum of 8 and 12 is "); System.out.println(8 * 12);

Logic error: the program does not produce the desired result

Which one of the following refers to a number constant that appears in code without explanation?

Magic Number

Select a condition to complete the code fragment below, which is designed to simulate one hundred coin tosses. for (int j = 1; j <= 100; j++) { if ( ______________ ) { System.out.println("Heads"); } else { System.out.println("Tails"); } }

Math.random() > 0.5

What is the output of the following code snippet? boolean attendance = false; String str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } System.out.println(str);

Maybe

What output is generated by the statements below? System.out.println("New"); System.out.print("York");

New York

What will be the output of the following code snippet? boolean token = false; while (token) { System.out.println("Hello"); }`

No output after successful compilation.

How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); }

Only one time.

What (if any) type of error occurs with the following code if the user input is ABC? public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); String str = in.next(); int count = Integer.parseInt(str); System.out.println("Input is " + count); }

Run-time error

Show the output generated by the code segment below. String destination = "Empire-State-Building"; System.out.print(destination.substring(7, 12));

State

What kind of error is created by the following code snippet? System.outt.println("Hello");

Syntax error: the program will not compile

Assuming that in is a properly initialized object of the Scanner class, which of the following code segments is most appropriate for reading an integer value to be entered by the user?

System.out.print("Please enter the number of tests: "); int tests = in.nextInt();

Which of the following can be used to display float variable fraction with two decimal digits?

System.out.printf("%.2f", fraction);

Which one of the following statements displays the output as 54321.00?

System.out.printf("%8.2f", 54321.0);

Which of the following statements represents a logic error?

System.out.println("The sum of 5 and 6 is 10");

What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1? public static void main(String[] args) { double sum = 0; int count = 0; double salary = 0; double average = 0; Scanner reader = new Scanner(System.in); System.out.println("Enter salaries (-1 to stop): "); while (salary != -1) { salary = reader.nextInt(); if (salary != -1) { sum = sum + salary; count++; } } if (count > 0) { average = sum / count; System.out.println("The Average Salary: " + average); } else { System.out.println ("No data!"); } return 0; }

The Average Salary: 30

What is the problem with the following algorithm? Repeat a number of times Add sales amount to total sales.

The algorithm is ambiguous because it does not specify how many times to repeat the Add statement

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

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

What is wrong with the following code snippet? public class Area { public static void main(String[] args) { int width = 10; height = 20.00; System.out.println("area = " + (width * height)); } }

The code snippet uses an undeclared variable.

The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } }

The code snippet will display the desired result

What happens to the fractional part when a division is performed on two integer variables?

The fractional part is discarded.

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

The new pay is 27500

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

The new price is 22.5

These two lines of code do not produce the same output. Why? System.out.println(7 + 3); System.out.println("7 + 3");

The quotes cause the second expression to be treated as a string.

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

The variables i and sum

Assuming that str is a String variable, what is the purpose of the code segment below? int count = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch != '*') { count++; } }

To count the number of characters in str that are not asterisks.

What is the purpose of the following algorithm, written in pseudocode? num = 0 Repeat the following steps 10 times Input var1 If var1 > num Num = var1 Print num

To find the highest among 10 numbers

Which of the following options refers to the technique of simulating program execution on a sheet of paper?

Tracing

What is the output of this code snippet? double average; int grade1 = 87; int grade2 = 94; // System.out.print("The average is " + (grade1 + grade2) / 2.0); System.out.print("The average is " + average);

Unpredictable result

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.

Assuming that weight has value 157.8 and age has value 35, what output is generated by the statement below? System.out.printf("Weight: %6.2f, Age: %4d", weight, age);

Weight: 157.80, Age: 35

In a flowchart, a decision is represented by

a diamond with two outcomes

Assuming that the user provides 99 as input, what is the output of the following code snippet? int a; int b; a = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); b = in.nextInt(); if (b > 300) { a = b; } else { a = 0; } System.out.println("a: " + a);

a: 0

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

bcde

Which of the following represents a correct way to declare and initialize a boolean variable valid?

boolean valid = false;

Which of the following is the Java equivalent of the following mathematical expression? c = 2π . radius

c = 2 * Math.PI * radius;

Which is the Java equivalent of the following mathematical expression? c = √(a2 + b2)

c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

The part of the computer that executes instructions is the:

central processing unit

Assuming that variables dollars and cost have been declared, which of the following assignment statements is INVALID?

dollars + 100 = cost;

Suppose variable average is to contain the average (including possible decimal values) of four integer variables: a, b, c, and d. Which of the following statements would perform the correct operation?

double average = (a + b + c + d) / 4.0

A store provides 10 percent discount on all items with a price of at least $100. No discount is otherwise applicable. Which of the following DOES NOT correctly compute the discount?

double discount = 0.10 * price; if (price <= 100) { discount = 0; }

Which of the following is considered a loop with a bound that could be problematic?

for (i = 1; i != n; i++)

The physical computer and peripheral devices are collectively called the:

hardware

What is the output of the following code snippet? String str1 = "her"; String str2 = "cart"; if (str1.compareTo(str2) < 0) { System.out.print(str2); } else { System.out.print(str1); }

her

What are the values of i and j after the following code fragment runs? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i=" + i + ", j=" + j);

i = 1951, j = 0

What is the output of the code snippet given below? int i; int j = 0; for (i = 0; i < 5; i++) { if (i % 2 == 0) { i = i + 2; j++; } else { i++; j = j + 2; } j++; } System.out.println("i=" + i + ", j=" + j);

i=5, j=5

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

if (s.equals("5"))

Programs that are not running are usually stored

in secondary storage.

Insert the missing condition in the following code fragment. The code is intended to compute the product of several integers entered by the user. The loop should stop when the user enters something other than an integer. int product = 1; while ( ____________ ) { int value = in.nextInt(); product = product * value; }

in.hasNextInt()

Which of the following represents a correct way to declare and initialize a variable named counter?

int counter = 1;

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

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

Select an expression to complete the code segment below. The nested loops should display the pattern: 2,0 2,2 2,4 3,0 3,2 3,4 for (int j = 2; j <= 3; j++) { for (int k = 0; k <= 4; __________ ) { System.out.print(j + "," + k + " "); }

k = k + 2

Consider the following code snippet. Assuming that the user enters first 20 and then 12 as the two input values, what is the output of the code snippet? int num1 = 0; int num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num1 = in.nextInt(); System.out.print("Enter a number: "); num2 = in.nextInt(); if (num1 < num2) { num3 = num1; } else { num3 = num2; } if (num1 < num2 + 10) { num4 = num1; } else if (num1 < num2 + 20) { num5 = num1; } System.out.println("num1 = " + num1 + " num2 = " + num2 + " num3 = " + num3 + " num4 = " + num4 + " num5 = " + num5);

num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0

Insert the missing condition in the following code fragment. The code is intended to compute and display the sum of the first 100 positive integers. int number = 1; int sum = 0; while (__________) { sum = sum + number; number++; } System.out.print(sum);

number <= 100

Select the assignment statement that correctly implements the algorithmic step below: number of pairs = integer part of (total width - tile width) / (2 x tile width)

numberOfPairs = (int)((totalWidth - tileWidth) / (2 * tileWidth));

How do you compute the length of the string str?

str.length()

How do you extract the first 5 characters from the string str?

str.substring(0, 5)

Characters that are grouped together between double quotes (quotation marks) in Java are called

strings

Every statement in Java must be terminated with

the semi-colon character ;

Insert the missing condition in the following code fragment. The code is intended to add 5 to variable shippingCost if the value of variable weight is at least 10. if (__________) { shippingCost = shippingCost + 5; }

weight >= 10

Assuming that the user provides 3 as input, what is the output of the following code snippet? int x; int y; x = 0; System.out.print("Please enter y: "); Scanner in = new Scanner(System.in); y = in.nextInt(); if (y > 0) { x = 2 * y; } else { x = 2 + x; } System.out.println("x: " + x);

x: 6


Related study sets

AP US History Benchmark Review 1 (Practice Questions)

View Set

Chapter 38 Agents to control blood glucose

View Set

Study for Physics : exam 3A, 2A & homework

View Set

Statistics 2- Regression Diag. Multicollinearity

View Set