AP Comp Sci Semester 1 Final

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

Which of the following symbols is used to enclose a String literal?

" "

A multi-line comment begins with the symbol /*. Which of the following symbols marks the end of a multi-line comment?

*/

The following expression evaluates to this (1 / 2) * 6

0

What is printed by the following code segment? for(int i = 0; i <= 2; i++) { if(i % 2 == 0) System.out.print(i); break; }

0

What is printed by the following code segment? int c = 0; while(c < 6) { System.out.print(c + " + " + c); c++; }

0 + 01 + 12 + 23 + 34 + 45 + 5

What would the following expression result to, in Java? (12 +2) / 18/3.0

0.26

What is the appropriate way to construct a Scanner object in Java?

1. import statement 2. object construction 3. prompt user for input 4. capture user input

The following expression evaluates to this 7.0 * (1 / 4)

1.75

Evaluate the following arithmetic expressions: 225 % 100 / 3 + 2.6

10.9

Convert the following decimal number to binary: 217 dec to ? bin

1111111

Convert the following hexadecimal number to decimal: 7F hex --> ? dec

127

What is the largest number a 7 bit binary sequence could represent?

127

What is printed by the following code segment? int x = 15; int count = 1; while(count <= x) { if(x % count == 0) { System.out.print(count); x -= count; } count += 1; }

1293

public class PowersOfTwo { public static void main(String[] args) { double value = 1; int power = 0; while(power <= 12) { value = Math.pow(2, power); power = power + 1; System.out.println(value); } } } Use the above code to determine the number of times the while loop will execute.

13

What is printed by the following code segment? int count = 1; while(count < 7) { if(count % 2 != 0) System.out.print(count); count++; }

135

In the hexadecimal number system, the letter 'E' represents what number in decimal?

14

Convert 10100011 bin to ? dec

163

public class Sum100 { public static void main(String[] args) { int sum = 0; int num = 4; while(num <= 100) { sum = sum + num; //takes the sum of the numbers num = num + 1; //incrementing num by 1 } System.out.println("The total sum is " + sum); } } What would the variable sum be on the 6th iteration?

19

What is printed by the following code fragment? int x = 3; x *= 3; x /= 4; System.out.println(x);

2

What is printed by the following code segment? for(int i = 2; i <= 8; i++) { if(i % 2 == 0) System.out.println(i); }

2 4 6 8

What is the result of the following code segment? int x = 4; int y = 7; x += x + y * 2; System.out.println(x);

22

What is printed by the following code segment? int c = 0, s = 0; while(true) { s += c; if(s > 20) break; c += 4; } System.out.println(s);

24

What is printed by the following code segment? int x = 0; x += 15; x *= 3; x %= 6; System.out.println(x);

3

Consider the following code segment, which uses the variables r, s, and t. r ← 1 s ← 2 t ← 3 r ← s s ← t DISPLAY (r) DISPLAY (s) What is displayed as a result of running the code segment?

3 3

What is printed as a result of executing the following code segment? int x = 3; System.out.println("Total: " + x + 4.0);

34.0

(Pseudocode) Assume that the variable x is assigned the following value: x ← RANDOM(0,10) Which of the following code segments would ensure that x is a random number between 2 and 32 ([2, 32])? Note that RANDOM(x,y) returns a random number in the range [x,y].

3x+2

Consider the following code fragment: int sum = 0; int outer = 1; while (outer <= 2) { int inner = 1; while(inner <= outer) { sum += inner; inner++; } outer++; } System.out.println(sum); What is printed as a result of executing the code segment?

4

What should be solved second in the following expression? 3 + 4 * 12 / 2^2

4*12

The following expression evaluates to this 2 + 3.0

5.0

What would the following expression result to, in Java? (13 * 2) / 4

6

What would the following expression result to, in Java? 16 / 4 + (32 * 2) / 16.0

8

What is printed by the following code fragment? double x = 8; double y = 1 / 4 System.out.println(x + y);

8.25

What is the appropriate syntax, in Java, of declaring and assigning an int variable?

<data type> <variable name>; ex. int myVariable;

Convert 10110011 bin to ? hex

B3

What is displayed as a result of running the following code segment? a ← 30 b ← 2 c ← 5 IF (a < 0) { DISPLAY ("Negative") } ELSE { IF (a MOD b = 0) { DISPLAY ("Even") } ELSE { IF (a MOD c = 0) { DISPLAY ("Factor of five") } ELSE { DISPLAY ("Odd") } } }

Even

Given the following code segment: int wake = 8; int time = input.nextInt(); // get time from user if(wake < time) { System.out.println("Get out of bed! You're late!"); } System.out.println("Have a good day!"); What is printed to the user if he or she enters a time of 8?

Have a good day!

public class Variables { public static void main(String[] args) { int dog = 24; int cat; cat = 65; System.out.println("Hello," + cat); } } What will result in the following code segment above?

Hello, 65

What is true about if statements with multiple alternatives? Choose two answers. I. Provides more control and greater efficiency II. If it does contain an else, it is possible that ALL of the bodies will be executed III. If it does not contain an else, it is possible that none of the bodies will be executed IV. Braces are optional with single and multi-lined statements

I, III

Given the following code segment: int year = input.nextInt(); // get year from user if (year <= 1999) { System.out.println("Let's drive!"); } else { System.out.println("Wait a bit longer..."); } System.out.println("Age check complete."); What is printed to the user if he or she enters a year of 1999?

Let's drive!

Given the following class header: public class MoonWeight { ... } For the program MoonWeight to compile, it must be saved in which of the following files?

MoonWeight.java

(Pseudocode) In the program below, the initial value of x is 5 and the initial value of y is 10 IF (x < 0) { DISPLAY ("Foxtrot") } ELSE { IF (x > y) { DISPLAY ("Hotel") } ELSE { IF (y > 0) { DISPLAY ("November") } ELSE { DISPLAY ("Yankee") } } } What is displayed as a result of running the program?

November

Given the following code segment: int speed = input.nextInt(); // get speed from user if(speed <= 60) { System.out.println("Safe travels!"); } else if(speed < 65) { System.out.println("Slow down! Be careful!"); } else if(speed > 65) { System.out.println("Caught speeding! :("); } What is printed if the user enters 60?

Safe travels!

Given the following code segment: int speed = input.nextInt(); // get speed from user if(speed <= 60) { System.out.println("Safe travels!"); } else if(speed < 65) { System.out.println("Slow down! Be careful!"); } else if(speed > 65) { System.out.println("Caught speeding! :("); } What is printed if the user enters 64?

Slow down! Be careful!

Given the following Scanner object: Scanner input = new Scanner(System.in); Which of the following can be used to obtain a String from the user?

String name = input.nextLine();

Given the following class header: public class TempConverter { ... } For the program TempConverter to compile, it must be saved in this file

TempConverter.java

boolean snowDay = true; if(!snowDay) System.out.println("Stay inside!"); System.out.println("The weather outside is frightful!"); What is printed to the user?

The weather outside is frightful!

Given: Random randy = new Random(); int x = randy.nextInt(10); x will be a random number from which of the following ranges?

[0, 10)

What does it mean for a programming language to be a statically typed language?

all variables must have data type and once it is declared, only that type of data can be stored in that variable

In Java, the equal sign (=) is known as the ________ operator.

assignment

This operator is used to assign a value to a variable.

assignment operator

A sequence of multiple bits is simply known as a ______________________.

byte

Given the following code segment: public class Vote { public static void main(String[] args) { int age = 20; if(age >= 18) { System.out.println("Can vote."); } // A else { System.out.println("Can't vote"); } // B } // C } // D The scope of the variable age ends with which brace?

c

This convention is a naming convention in which multiple words are combined and uppercase letters are used to distinguish words.

camel case

A syntax error is also known as a ________________________

compile time error

While-loops and if-statements are in the same category of _____________________.

control statements

This primitive data type is capable of storing floating-point numbers.

double

When considering a variable's data type, which would be the most appropriate for storing GPA?

double

What is printed by the following code segment? int x = 6; for(int i = 1; i < x; i++) { if(x % i == 0 && i % 2 == 0) break; } System.out.println(i);

error

An int data type is capable of storing floating-point numbers (true/false)

false

One (1) bit is comprised of eight (8) bytes. (true/false)

false

When executed, the condition of an if-statement is checked. If the condition is false, all of the statement(s) in the body will be executed. (true/false)

false

x-- is functionally equivalent to x = x - x. (true/false)

false

Scanner can be used to obtain input from this and this.

from a file or keyboard directly

/*missing code*/ public class TempConverter { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter in a temperature in Fahrenheit: "); double F = input.nextDouble(); double C = (5.0 / 9.0) * (F - 32); System.out.println(F + " Fahrenheit = " + C + " Celsius"); } } In the code above the symbol /*missing code*/ is showing that code is missing. What best represents the missing code?

import java.util.Scanner;

In order to gain access to libraries outside the default library in Java, this statement is used.

import statement

Which of the following has the greatest impact on readability?

indentation

import java.util.Scanner; public class SphereVolume {public static void main(String[] args) {Scanner goldfish = newScanner(System.in); System.out.println("Please enter a radius: "); double radius = /*missing code*/; double volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3); System.out.println("Volume = " + volume);}} Fill in the /*missing code*/ in the above code. Which of the following code segment would best represent the missing code?

input.nextDouble

This data type consumes half as much space as a double.

int

What is the data type result of dividing an int and int together; for example: 5 / 7?

int

Which of the following data types would be most appropriate for storing the number of students in a classroom?

int

List all primitive data types

int, double, boolean, char, byte, short, float, long

A single execution of the loop's body is known as which of the following?

iteration

Each time the body of a loop is executed is known as a(n) ________.

iteration

______________________ are used to compare the relationship between two primitive data types of comparable types.

logical operators

The compiler will ignore indentation as long as it doesn't appear in these two place

middle of words and quoted strings

Either of these two Scanner methods return a String.

next(); nextline()

Given the following code segment: int speed = input.nextInt(); // get speed from user if(speed <= 60) { System.out.println("Safe travels!"); } else if(speed < 65) { System.out.println("Slow down! Be careful!"); } else if(speed > 65) { System.out.println("Caught speeding! :("); } What is printed if the user enters 65?

nothing

This statement is used to construct a Scanner object and assign it to a Scanner variable called input.

object construction

public class TooMuch { public static void main(String[] args) { int x = -2147483648 - 10; System.out.println(x); } } What would occur with the code above?

overflow

Java sets aside certain keywords that have a special meaning or action known as ____________

reserved words

________ is a lossless compression technique in which a sequence of duplicate data is replaced with one instance and a count.

run length coding

The _________________________ is the region in a program in which a variable exists and can be accessed or modified.

scope of variable

There are two types of commenting in Java, what are they?

single line and multi line comments

This data type is used to store text and is not a primitive data type.

string

In Java, every variable's data type is either a primitive data type or a(n) ______________

string variable

After all of the statements in the body of a loop are executed, the condition will be checked again. (true/false)

true

Lossless compression should be used when loss of information could pose a problem, such as with text documents and spreadsheets. (true/false)

true

The body of a while-loop will always execute at least one time. (true/false)

true

The braces that indicate the body of a loop can be omitted if the loop contains only one statement. (true/false

true

The jpeg image format uses lossy compression to reduce the file size of images. (true/false)

true

The second statement in the header of a for-loop is the loop's condition. (true/false)

true

The variable's data type cannot change after it is declared. (true/false)

true

{ public static void main(String[] args) { double x = Math.PI; double y = 3.14; boolean check = x > y; if(check) System.out.println(check); else { System.out.println(x); } } } What would the above code result to?

true

The following expression evaluates to this (9 / 3) / (1 / 2)

undefined

public class PowersOfTwo { public static void main(String[] args) { double value = 0; int power = 0; while(/*missing code*/) { value = Math.pow(2, power); power = power + 1; System.out.println(value); } } } Which condition would cause the following code segment would cause the program to continuously loop?

value < 0

(Pseudocode) The code fragment below is intended to display "odd" if the positive number num is odd. IF (<MISSING CONDITION>) { DISPLAY ("odd") } Which of the following can be used to replace <MISSING CONDITION> so that the code fragment will work as intended?

x % 2 == 1

What is printed as a result of executing the following code segment? double x = 2; System.out.println("x + x");

x + x


Kaugnay na mga set ng pag-aaral

Chapter 37: Drugs Therapy for Peptic Ulcer Disease and Hyperacidity

View Set