AP Computer Science- Chapter 2: Objects and Primitives

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the output produced by the following statement? Explain. System.out.println( "He ate\n\t lots of food" + " post\nand insists\n\ the saw \"ghost\"");

(SPACING MATTERS!!!!) He ate (new line) (tab) losts of food post and insists (new line) (tab) he saw "ghost"

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a % b % c % d

1. a % b 2. % c 3. % d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a % b / c * d

1. a % b 2. / c 3. * d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. (a + b) * (c / d) % e

1. a + b 2. (c / d) 3. (a + b) * (c / d) 4. % e

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. (a + b) * c + d * e

1. a + b 2. * c 3. d * e 4. + de

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a - b - c - d

1. a -b 2. - c 3. - d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a / b * c * d

1. a / b 2. * c 3. * d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a - b + c - d

1. a- b 2. + c 3. -d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a % (b % c) * d * e

1. b % c 2. a % 3. * de

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a - ((b - c) - d)

1. b - c 2. - d 3. a -

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. (a - (b - c)) - d

1. b - c 2. a - 3. - d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a - (b - c) - d

1. b - c 2. a - 3. - d

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a + ( b - c) * d - e

1. b - c 2. * d 3. + a 4. - e

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a + b / c * d

1. b / c 2. * d 3. + a

For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a + b / c / d

1. b / c 2. / d 3. + a

What is the result of 13 / 4 when evaluated in a Java expression? Explain.

3. The result is an integer because both operands are integers.

What is the result 19%5 when evaluated in a Java expression? Explain.

4. The remainder operator % returns the remainder after dividing the second operand into the first. Five goes into 19 three times, with 4 left over.

What output is produced by the following statement? Explain. System.out.println ("50 plus 25 is " +50+25);

50 plus 25 is 5025 First the string "50 plus 25" is concatenated with the string "50"; since one of the operands associated with the "+" operator is a string, the other is treated as a string. Then the string "50 plus 25 is 50" is concatenated with the string "25".

What is a string literal?

A string literal is a sequence of characters that appear in double quotation marks.

What is a variable declaration?

A variable declaration gives the name of a variable and the type of data that it can contain. A declaration may also have an initialization, which gives the variable an initial value.

Why are widening conversions safer than narrowing conversions?

A widening conversion does not cause information to be lost. Information is more likely to be lost in a narrowing conversion, which is why narrowing ones are considered to be less safe.

How can we represent a primitive value as an object?

A wrapper class is defined in the Java standard class library for each primitive type. In situations where objects are called for, an object created from a wrapper class may suffice.

What is an escape sequence? Give some examples.

An escape sequence is a series of characters that begins with the blackslash(\). Examples: \n represents the newline character and \" represents the quotation character (instead of using it end a string)

How many values can be stored in an integer variable?

An integer variable can store only one value at a time.

Why is an object an example of abstraction?

An object is abstract because the details of the object are hidden from, and largely unimportant to, the user of the object. Hidden details help us manage the complexity of the software.

Consider a program that converts hours, minutes, and seconds into the total number of seconds. Assume the number of hours, minutes, seconds have been read into the variables hours, minutes, and seconds, respectively, and that the variable totalSeconds has been properly declared. Which of the following calculates the total number of seconds? a) total Seconds = hours * minutes *( 60 + minutes * 60 + seconds; b) totalSeconds = hours * 3600 + minutes * 60 + seconds; c) totalSeconds = hours + minutes * 60 + seconds * 3600; d) totalSeconds = hours % 3600 + minutes % 60 + seconds; e) totalSeconds = hours / 3600 + minutes / 60 + seconds;

B, totalSeconds = hours * 3600 + minutes * 60 + seconds;

What is the difference between the print and println methods?

Both print and println methods of the System.out.object write a string of characters to the computer screen. The difference is after printing the characters, the println does a return so that what's next is printed on the next line.

Consider the following variable declarations. String s = "crunch"; int a = 3, b = 1; What is printed by the following statements? System.out.print(s + a + b); System.out.println(b + a + s); a) crunch44crunch b) crunch413crunch c) crunch314crunch d) crunch3113crunch e) Nothing is printed due to a runtime error

C, c) crunch314crunch

Consider the following variable declarations. int a = 5, b = 4; double x = 5.0, y = 4.0; Which of the following expressions have the value 1? i) (int) x / y ii) a /b iii) a % b Answers: a) ii only b) i and ii only c) i and iii only d) ii and iii only e) i, ii, and iii

D, ii and iii only.

What is a parameter?

Data that is passed into a method. The method usually uses that data.

T/F: In the declaration int num = 2.4; the 2.4 will automatically be converted to an int and num will get the value.

False, the declaration int num = 2.4 will not automatically be converted to an int and num won't get the value.

T/F: A string literal appears inside single quotation marks.

False. A string literal appears inside double quotation marks.

What output is produced by the following code fragment? Explain. System.out.print ("Here we go!"); System.out.println ("12345"); System.out.print ("Test this if u not sure."); System.out.print ("Another."); System.out.println(); System.out.println ("All done.");

Here we go!12345 (*insert new line) Test this if u not sure.Another. (*insert new line) All done. After printing its data, the println method moves to the next line of output, whereas the print method does not. A println statement with no data has the effect of moving down to the next line.

What are the primary concepts that support object-oriented programming?

Primary concepts are objects, classes, encapsulation, and inheritance. An object is defined by a class, which contains methods that define the services the objects preform. Objects store and manage their own data. Inheritance is a technique in which one class can be created from another.

What is primitive data? How are primitive data types different from objects?

Primitive data are basic values such as numbers or characters. Objects are more complex and usually contain primitive data that help define them.

What output is produced by the following code fragment? String m1, m2, m3; m1 = "Quest for the Holy Grail"; m2 = m1.toLowerCase(); m3 = m1 + " " + m2; System.out.println (m3.replace('h', 'z'));

Quest for tze Holy Grail quest for tze zoly grail

Write a single statement that computes and prints the absolute value of total.

System.out.println ( Math.abs(total));

Explain the following programming statement in terms of objects and the services they provide: System.out.println("I gotta be me!");

The System.out object has a println method which accepts a string, enclosed in parentheses and quotation marks, which it displays on the monitor.

What does the new operator do?

The new operator creates an object of a class.

What is operator precedence?

The set of rules that dictates the order in which operators are evaluated in an expression.

Example of Parameter

The string of characters a println method prints. Or the two numbers the Math.pow method uses.

What is wrong with the following program statement? How can it be fixed? System.out.println("To be or not to be, that is the question.");

The string to be printed is not all on one line. The problem can be fixed by using the string concatenation operator (+) or by using a print statement for part of the string and a println statement for the remainder of the string.

T/F: In Java, Integer is a class, whereas int is a primitive type.

True, integer is a class and int is a primitive type.

T/F: Assuming generator is an object of the Random class, the call generator.nextInt(8) will generate a random number between 0 and 7 inclusive.

True, since the generator is an object of the Random class, the call generator.nextInt(8) will generate a random number between 0 and 7 inclusive.

T/F: Widening conversions can happen automatically, such as in the expression 1 + 2.5 where 1 is converted to a double.

True, widening conversions can happen automatically

T/F: The operators *, /, % have precedence over + and -.

True. *, /, % have precedence over + and -.

T/F: An object is an abstraction, meaning that the user doesn't need to know the details of how it works.

True. An object is an abstraction.

T/F: In order to include a double quotation mark (") or a backslash (\) in a string literal, we must use an escape sequence.

True. In order to include a double quotation mark (") or a backslash (\) in a string literal, we must use an escape sequence.

What is the result of the operation 30 % 4? a) 2 b) 3 c) 4 d) 7 e) 7.5

a) 2

Which of the following is a character literal? a) b b) 'b' c) "b" d) 2 e) 2.0

b) 'b'

Suppose we have a variable something that is a reference to a Thing object. How would we call the method doIt on out Thing object? a) doIt() b) something.doIt() c) doIt(something) d) something/doIt e) something(doIt)

b) something.doIt()

What will be printed by the following statement? System.out.println("Use a \"\\\""); a) Use a \"\\\" b) "Use a "\"" c) Use a "\" d) Use a \"\"" e) Use a "\\"

c) Use a "\"

The expression "number" + 6 + 4 * 5 produces which of the following string literals? a) "number645" b) "number105" c) "number50" d) "number620" e) "number26"

d) "number620"

Which statement would we use to create an object from a class called Thing? a) Thing something; b) Thing something = Thing(); c) Thing something = new Thing; d) Thing something = new Thing(); e) new Thing() = something;

d) Thing something = new Thing();

The expression 1 / 4 is equal to which of the following? a) 1.0 / 4.0 b) (double)2 / 8 c) 0.25 d) (int)1.0 / 4.0 e) 1 / (int)4.0

e) 1 / (int)4.0

Which keyword is used to declare a constant? a) int b) double c) MAX d) constant e) final

e) final

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = (double) (num1 / num2)

fResult = 0.0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = (int) ((double) num1 / num2)

fResult = 0.0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = (double) num1 / num2

fResult = 0.625

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = num1 / (double) num2

fResult = 0.625

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = val1 / val2

fResult = 1.3302034...

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = (int) (val1 /num4)

fResult = 3.0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = num3 / num4

fResult = 3.0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = val 1 / num4

fResult = 3.4

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; fResult = num1 / num4

fResult = 5.0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num1 / num2

iResult = 0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num2 % num4

iResult = 0

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num3 % num2

iResult = 17

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num3 % num4

iResult = 2

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = (int) (val1 / num4)

iResult = 3

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num3 / num4

iResult = 3

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num1 / num4

iResult = 5

Given the following declarations, what result is stored in each of the listed assignment statements? int iResult, num1 = 25, num2 = 40, num3 = 17, num4 =5; double fResult, vall =17.0, val2 = 12.78; iResult = num2 % num3

iResult = 6

Write code for creating a random number between 10 and 100 inclusive?

int r = (int)(Math.random() * 100) + 1

Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3.

num3 = Math.sqrt(num1 + num2);

Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following ranges, including the endpoints. 1 to 10

rand = (int)(Math.random() * 10) + 1;

Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following ranges, including the endpoints. 0 to 10

rand = (int)(Math.random() * 10);

Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following ranges, including the endpoints. -10 to 15

rand = (int)(Math.random() * 15) - 10;

Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following ranges, including the endpoints. 25 to 50

rand = (int)(Math.random() * 50) + 25;

Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following ranges, including the endpoints. 1 to 500

rand = (int)(Math.random() * 500) + 1;

Write code to declare and instantiate an object of the Random class (call the object reference variable rand). Then write a list of expressions using the nextInt method that generates random numbers in the following ranges, including the endpoints. 0 to 500

rand = (int)(Math.random() * 500);

Which of the following instantiates a String Object? a) String word; b) word = new String("the"); c) word.length(); d) word = name; e) String word = name;

word = new String("the");


Conjuntos de estudio relacionados

Psychology II: Chapter 13: Social Psychology Learning Curve

View Set

Pharm Final Quiz/Practice Questions-Antibiotics

View Set

Case Study: Look- and Sound-Alike Medications Exam

View Set

Anatomy (previous test/quiz answers)

View Set

Chapter 2: Conceptual Framework for Financial Reporting (Questions)

View Set

Менеджмент і маркетинг в ЗМІ

View Set