Quiz 1 Solutions - incompatible data types and will raise a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 302 Section 3 Quiz 1 Solutions 2006-09-15 Name: 1 (1 point) Which of the following blocks of code will cause a compile-time error? A int a = 1; int b = 2; double c = a/b; The expression double c = a/b; is valid Java code because the runtime will perform integer division, discarding any fractional remainder. In this case, the variable c will be given the value 0 . B String s1 = "Hello"; String s2 = "World"; s2 = s1; System.out.println(s2); Assigning the string referenced by s1 to the variable s2 will not cause an error; it will just cause s2 to point to the same string. This block of code will print the text Hello , and the other String object will eventually be reclaimed by the garbage collector. C double d1 = 5.0 / 2; double d2 = d1 + "42"; System.out.println(d2); This is the correct answer. Because Java is a strongly typed language, it will not automatically convert strings to numbers, even when the string is a valid numerical representation. The compiler will recognize that the second line is trying to add
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: incompatible data types, and will raise a compile-time error. 2 (2 point) Write a series of statements that creates a variable of the Beeper type, assigns to it a new Beeper object, then sends the object the message “beep” with the single argument 5. Writing a line of that created the object was worth one point; writing a line that called the beep method was worth another point. The following answer would have received both points: Beeper beeperVariable = new Beeper(); beeperVariable.beep(5); 3 (2 point) On the following chart, mark where Java’s type-handling system belongs. 1 6-Dynamic Static Weak Strong x Java is a staticly-typed language because the type of every variable is declared and known at the time of compilation. It is a strongly-typed language because data types cannot be automatically converted to other types (e.g. the string to double conversion in Question 1). Correct placement on each axis was worth one point. 2...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Quiz 1 Solutions - incompatible data types and will raise a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online