Comp Sci A Unit 1 - Primitive Types (Output in Java, User input and variables, Data types)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Strings are specified by

" "

What is printed by the following code? int y = (13 - 7) / 4; System.out.println(y);

1

To find the last two digits of a number you MOD by _______. So for example the last two digits of 7346 is 46.

100

What is the code used to print something on the screen?

System.out.print(" ");

Which line of code properly casts a double to an integer? a) int n = (int) 6.71; b) int n = (double) 6.23; c) double n = (int) 3.14; d) int n = int(34.5);

a) int n = (int) 6.71;

what is instantiating?

creating objects (objects created from the Scanner class)

Which line of code correctly declares and initializes the variable x to a value of 3? a) int x : 3; b) Integer x = new Integer(x); c) x = 3 d) int x = 3;

d) int x = 3;

What color text signifies that the code is a comment and won't be run?

green

Consider the following code segment. int j = 10; int k = 8; j += 2; k += j; System.out.print(j); System.out.print(" "); System.out.println(k);

12 20

Which of the following arithmetic expressions evaluates to 1 ? 2 / 5 % 3 2 / (5 % 3) 2 / 5 + 1

II and III

Consider the following code: long y = 9223372036854775807; y = y + 1; System.out.println(y); What number does the program print when compiled and run?

This program crashes and raises a longOverflow exception

Which of the following are examples of primitive data types? a) boolean b) char c) Scanner d) int e) Arrays

a, b, d

What is the largest value you can store in a long variable? a) 1,024 b) 9,223,372,036,854,775,807 c) 2,147,483,647 d) 256

b

What data type should you use to hold whether a person passed or failed a class?

boolean

the directions we give in programming are called

commands

Which of the following is NOT a valid string in Java? a) "hello" b) "@#$" c) "a" d) "123" e) In Java, these are all valid strings

e) In Java, these are all valid strings

List primitive data types

int, double, boolean

What does the import statement do? importJava.util.Scanner;

introduces the class before the rest of the code

What is concatenation>

joining literal strings with variables (use +)

In the following code, what do the periods act as? System.out.println(" ");

member selectors (separate)

To output: Hello There You would need to use: System.out.______("Hello") System.out.print("There");

println

String variables can be concetenated with

string literals

What is 61 % 4?

1

Consider the code segment below. int x = 10; int y = 20; System.out.print(y + x / y); What is printed as a result of executing the code segment?

20

What is 13 % 7?

6

What is the output when the following code is executed? int a = 93; double x = 89.86732; System.out.println((int) x);

89

What will happen if the computer fails to compile your code?

an error message will be displayed

In the following code, what is the equal sign? Scanner scan = new Scanner(System.in);

assignment operator (initializes or changes the value of the variable)

Consider the following code segment. int x = /* initial value not shown */; int y = /* initial value not shown */; int z = x; z /= y; z += 2; Which of the following best describes the behavior of the code segment? a) It sets z to 2. b) It sets z to x. c) It sets z to (1 / y) + 2. d)It sets z to (x / y) + 2. e) It sets z to (x + 2) / y.

d) It sets z to (x / y) + 2.

Which of the following is a legal variable name in Java? a) a name b) n.1 c) 1test d) value e) String

d) value

Consider the following code segment. System.out.println(hello); // Line 1 System.out.print(world); // Line 2 The code segment is intended to produce the following output but does not work as intended. hello world Which of the following changes can be made so that the code segment produces the intended output?

enclosing hello in line 1 and world in line 2 in quotation marks

Why does strings take up so much space in memory?

have to store each character separately

What is wrong with the following code? String s = nextLine( );

should be: String s = scan.nextLine( );

True or False. All statements in Java end with a semicolon

true

True or False. Reference data types can store several different types of info and thus don't get stored directly in the variable like primitive data types are

true

True or False. Strings are used for anything we don't do calculations with

true

True or False. Variables are like containers in memory

true

What is output when the following code is executed? double num = (0 - 3) * 5; System.out.println(num);

-15.0

Consider the following code segment. double d = 0.25; int i = 3; double diff = d - i; System.out.print((int)diff - 0.5); What is printed as a result of executing the code segment?

-2.5

What is output when the following code is executed? int x = -7; x++; x++; x--; System.out.println(x);

-6

What is used to mark a single line comment?

/ /

What is used to mark a block comment?

/* */

Consider the following code segment. double p = 10.6; double n = -0.2; System.out.println((int) (p + 0.5)); System.out.print((int) (n - 0.5)); What is printed as a result of executing the code segment?

11 0

What is output when the following code is executed? double num = 2.0 * (26.75 - 20); System.out.println(num);

13.5

What is output when the following code is executed? int x = 13; x++; x++; x -= 7; x *= 3; x--; System.out.println(x);

23

To tell if a number is divisible by 3, MOD by ______.

3

What is printed by the following code? int j = 33; j %= 6; System.out.println(j);

3

Consider the following code segment. double a = 7; int b = (int) (a / 2); double c = (double) b / 2; System.out.print(b); System.out.print(" "); System.out.print(c); What is printed as a result of executing the code segment?

3 1.5

Consider the following code segment. int a = 1; int b = 2; int c = 3; int d = 4; double x = a + b * c % d; What is the value of x when the code segment has been executed?

3.0

What is the output when the following code is executed? int a = 93; double x = 89.86732; System.out.println(a / 3);

31

Consider the following code segment. int num = 5; num *= 2; num %= 6; What is the value of num after the code segment is executed?

4

Consider the following code segment. int x = 0; x++; x += 1; x = x + 1; x -= -1; System.out.println(x); What is printed when the code segment has been executed?

4

consider the following code segment. System.out.print("Ready"); // Line 1 System.out.print("Set"); // Line 2 System.out.print("Go!"); // Line 3 The code segment is intended to produce the following output but may not work as intended. Ready Set Go! Which change, if any, can be made so that the code segment produces the intended output?

Changing print to println in lines 1 and 2

Consider the following code segment. System.out.print("Hello!"); System.out.println("How "); System.out.print("are "); System.out.print("you?"); What is printed as a result of executing the code segment?

Hello!How are you?

Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended? System.out.print("Hello"); System.out.print(Hello); System.out.print(He);System.out.print(llo);

I only

The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to ππ (pi) times the square of the radius. The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. /* missing code */ System.out.print(volume); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? double baseArea = pi * r * r;double volume = baseArea * h; double volume = pi * r * r;volume = volume * h; double volume = pi * r * r * h;

I, II, and III

Consider the following code segment. int x = 10; int y = 20; /* missing code */ System.out.print(top / bottom); Which of the following replacements for /* missing code */ will cause an ArithmeticException to occur? int top = x - y;int bottom = y - x; int top = 2 * x;int bottom = y - top; int top = x + y;int bottom = 2 * top;

II only

What is the code you should print when you want to scan input?

Scanner scan = new Scanner(System.in);

Different data types can store different amounts of data. List the data types in order from largest to smallest

String, double, int, boolean

A store sells rope only in whole-foot increments. Given three lengths of rope, in feet, the following code segment is intended to display the minimum length of rope, in feet, that must be purchased so that the rope is long enough to be cut into the three lengths. For example, for lengths of 2.8 feet, 3 feet, and 5 feet, the minimum length of rope that must be purchased is 11 feet. For these inputs, the code segment should display 11. As another example, for lengths of 1.1 feet, 3.2 feet, and 2 feet, the minimum length of rope that must be purchased is 7 feet. For these inputs, the code segment should display 7. double len1; double len2; double len3; double total = len1 + len2 + len3; int minLength = (int) (total + 0.5); System.out.print(minLength); Which of the following best describes the behavior of the code segment?

The code segment does not work as intended for some values of len1, len2, and len3. It could be corrected by casting len1, len2, and len3 to int before adding them together and assigning the sum to minLength.

Consider the following code segment. System.out.println("W"); System.out.println("X"); System.out.print("Y"); System.out.print("Z"); What is printed as a result of executing the code segment?

W X YZ

What is wrong with the following code? String s; System.out.println(s);

You cannot print a String without setting its value; the string, s, needs to be initialized with a value before it can be printed.

Executing which of the following lines of code would cause widening conversions to take place? Choose all answers that apply. a) double a = (double) 23 / 6; b) int a = (int) 11.4; c) double a = 19 / 7; d) int a = 13.7 * 3;

a and c

What is one reason we would use numeric casts in Java? a) There is a chance that you lose data when you convert from a larger data type to a smaller data type. b) Java is unable to determine the data types correctly at run time without it. c) There is no reason to ever use casts unless you want to. d) There is a chance that you lose data when you convert from a smaller data type to a larger data type. e) It is required by binary storage.

a) There is a chance that you lose data when you convert from a larger data type to a smaller data type.

Consider the following code segment, which is intended to display 6.0. double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product); Which of the following best describes the error, if any, in the code segment? A There are no errors and the code works as intended. B Either the numerator or the denominator of the fraction 1 / 2 should be cast as double. C The expression fact1 * fact2 should be cast as double. D The expressions 1 / 2 and 3 * 4 should both be cast as double. E The variables fact1 and fact2 should both be declared as int.

b

Which statement correctly declares a variable that can store a temperature rounded to the nearest tenth of a degree? A boolean patientTemp; B double patientTemp; C int patientTemp; D patientTemp = 0; E patientTemp = 0.0;

b

What is wrong with the code below? int guess = 42.07; a) Nothing the code is fine as-is. b) You cannot store a double in an int variable. c) The type should be "Double" not "int". d) You cannot store an int in a double variable. e) The variable should be a String. f) The type of the variable should be a float or a double.

b and f

Consider the following code segment. int x = 4; int y = 6; x -= y; y += x; Which of the following best describes the behavior of the code segment? A The value of x and the value of y have not been changed. B Both the value of x and the value of y have been decreased. C Both the value of x and the value of y have been increased. D The value of x has been decreased and the value of y has been increased. E The value of x has been increased and the value of y has been decreased.

b) Both the value of x and the value of y have been decreased

Suppose we had declared a String variable called myString in a program. Which of the following lines would assign this variable to contain the String "Hi there"? a) set myString to "Hi there" b) myString = "Hi there"; c) "Hi there" = myString; d) String myString "Hi there" e) myString: "Hi there";

b) myString = "Hi there";

A teacher determines student percentages in a course as the points a student earns divided by the total points available in the grading period. Points are awarded only in whole number increments, but student percentages are to be stored as decimals. The following code segment appears in a program used to compute student percentages. Points that a student earns are stored in pointsEarned, the total points available in the grading period are stored in totalPoints, and the student percentage is stored in percentage. int pointsEarned; /* missing code */ Which of the following is most appropriate to replace /* missing code */ in the program? A int totalPoints; int percentage; B double totalPoints; int percentage; C int totalPoints; double percentage; D int totalPoints; boolean percentage; E double totalPoints; boolean percentage;

c

Consider the following code segment, which is intended to display 0.5. int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans); Which of the following best describes the error, if any, in the code segment? A There is no error and the code works as intended. B The code should have cast the expression num1 / num2 to double. C The code should have cast either num1 or num2 in the expression num1 / num2 to double. D The code should have declared ans as an int. E The code should have initialized num1 to 5.0 and num2 to 10.0.

c

Which of the following would Java interpret as being a String? a) <p>Hello World</p> b) 'Hello World' c) "Hello World" d) ```Hello World```

c) "Hello World"

Consider the following code segment, which is intended to calculate the average of two quiz scores. double avg = 15 + 20; avg /= 2; Which of the following best describes the behavior of the code segment? a) the code segment stores 17 in avg because 17 is the result of the integer division of 35 by 2. b) the code segment stores 18 in avg because 18 is the result of the integer division of 35 by 2. c) the code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2. d) The code segment does not compile because int values cannot be assigned to double values. e) The code segment does not compile because a double value cannot be divided by an int value.

c) the code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2.

Consider the following code segment. System.out.print("cat "); System.out.println("dog "); System.out.println("horse "); System.out.print("cow "); What is printed as a result of executing the code segment?

cat dog horse cow

If you are owed $0.73 in change, which programming statement below would tell you how many quarters you should get? Assume that change is an int variable that stores the number of cents owed (73 in this case).

change/25

Which of the following correctly describes the difference between reference data types and primitive data types? a) Unlike primitive data types, reference data types cannot be stored by a variable. b) Primitive data types contain multiple pieces of data, while reference types only store one value at a time. c) Primitive data types cannot be read by a Scanner object and stored using a variable. d) Primitive data types are stored directly in a variable, while reference data type variables store a memory reference to where the data is stored.

d) Primitive data types are stored directly in a variable, while reference data type variables store a memory reference to where the data is stored.

________ is the process of finding and eliminating errors in code.

debugging

If we input a string when doing calculations, we'll get an

input mismatch exception, or runtime error (it will compile, but crash)

What are the rules for naming variables?

no spaces, must start with a letter, no reserve words

What does the Scanner class allow us to do?

take user input and incorporate it into programs

What are you doing when you declare a variable?

telling computer to allocate space in memory for something (tell the name so it knows how to find it and the type so it knows how much space to set aside)

In the following code, what does System represent? System.out.println(" ");

the class name

What will be printed to the screen when the following code runs? String s1 = "three"; String s2 = "five"; System.out.print(s1 + s2);

threefive

why are classes capitalized?

to separate them from variables

When variables are assigned a type, what is the computer told about?

what possible values can be stored (how big a box of memory should be allocated); what operations can be performed on the data

Consider the following code segment. int x; int y; x = 3; y = /* missing expression */; x = 1 + 2 * y; System.out.print(x); System.out.println(y); Which of the following can be used as a replacement for /* missing expression */ so that the code segment prints 94 ? A 3 B x C x - 1 D x + 1 E 1 - 2 * x

x + 1


Set pelajaran terkait

Hofstede's and Trompenaars' cultural dimensions

View Set

BIOL 100 CH 20 Concepts of Animal Structure

View Set

LECTURE EXAM III (chapters:14,16,17,20,21)

View Set

RS QA CH 5 Aggregating and Analyzing Performance Improvement Data

View Set

Chapter 7: Legal Dimensions of Nursing Practice

View Set