Lab5 Wooseok Kim wkim3@albany.edu www.cs.albany.edu/~wooseok/201
Question Answer Points 1 A or B 8 2 A 8 3 D 8 4 20 5 for class 10 for main 5 points for output 5 D or E 8 6 B 8 7 1 15 8 D 8 9 C 8 10 B 8
Question Answer Points 11 D 8 12 B 8 13 A 8 14 A or B or D 8 15 E 8 16 B 8 17 A 8 18 C 8 19 nextint( 21 ) + 20 20 10 for 21 10 for 20 20 25 10 for correct substring 10 for correct call to length() 5 for correct output
Question 1 Comments should be A) rephrase the code it explains in English B) be insightful and explain what the instruction s intention is C) only be included in code that is difficult to understand D) Be used to define variables whose names are not easy to understand E) all of the above
Question 2 A unique aspect of Java that allows code compiled on one machine to be executed on a machine of a different hardware platform is Java s A) bytecodes B) syntax C) use of objects D) use of exception handling E) all of the above
Question 2
Question 3 An error in a program that results in the program outputting $100 instead of the correct answer, $250 is A) A programmer error B) A syntax error C) A run-time error D) A logical error E) A snafu
Question 3 B) A syntax error Compile Error C) A run-time error 12 / 0 =? D) A logical error Incorrect Answer
Question 4 Write a Java program that will display on two separate lines your name and your major.
Question 4 (Grading Guideline) 5 points for having the keyword class and a reasonable class name in the right place 2 points each for static, void, main(), String[], args 5 points for output the name and the major
Question 5 Consider the following statement: This statement will output lines of text A) 1 B) 2 C) 3 D) 4 E) 5
Question 5 1st line 2nd line 3rd line 4th line
Question 5 1st line 2nd line 3rd line 4th line Real output has 5 lines because of /r /r: Carrage Return
Question 6 If x is an int and y is a float, all of the following are legal except which assignment statement? A) y = x; B) x = y; C) y = (float) x; D) x = (int) y; E) all of the above are legal
Question 6 If x is an int and y is a float, all of the following are legal except which assignment statement? A) y = x; B) x = y; int < float C) y = (float) x; D) x = (int) y; E) all of the above are legal
Question 7 What will be the result of a in the following assignment statement? Assume b = 5 and c = 10.
Question 7 (Grading Guideline) 15 pionts Up to 10 points partical credit if they have some correct work
Question 8 Which library package would you import to use NumberFormat and DecimalFormat? A) java.beans B) java.io C) java.lang D) java.text E) java.util
Question 9 QuestionNineAndTen computes A) The correct average of x, y and z as a double B) The correct average of x, y and z as an int C) The average of x, y, and z as a double, but the result may not be accurate D) The sum of x, y and z E) The remainder of the sum of x, y and z divided by 3
Question 9 QuestionNineAndTen computes C) The average of x, y, and z as a double, but the result may not be accurate int = x + y + z int = int / 3 However, average is double
Question 10 What is output if x = 0, y = 1 and z = 1? A) 0 B) 0.0 C) 0.66666666 D) 0.66666667 E) 0.67 double average = (0 + 1 + 1) / 3 = 2 / 3 = 0.0 if int average? The answer is 0
Question 11 In order to create a constant, you would use which of the following Java reserved words? A) private B) static https://www.javatpoint.com/static-keyword-in-java C) int D) final E) class
Question 12 In Java, a variable of type boolean will store either a 0 or a 1 A) true B) false
Question 12 In Java, a variable of type boolean will store either a 0false or a 1true A) true B) false
Question 13 In Java, a variable may contain A) a primitive value or an object reference B) a package C) a method D) a class E) any of the above
Question 14 (Dropped) What happens if you attempt to use a variable before it has been initialized? A) A syntax error may be generated by the compiler B) A runtime error may occur during execution C) A garbage or uninitialized value will be used in the computation D) A value of zero is used if a variable has not been initialized E) Answers A and B are correct
Question 14 (Dropped) What happens if you attempt to use a variable before it has been initialized? A) A syntax error may be generated by the compiler B) A runtime error may occur during execution D) A value of zero is used if a variable has not been initialized
Question 15 What is the function of the dot operator? A) It serves to separate the integer portion from the fractional portion of a floating point number B) It allows one to access the data within an object when given a reference to the object C) It allows one to invoke a method within an object when given a reference to the object D) It is used to terminate commands (much as a period terminates a sentence in English) E) Both B and C are correct
Question 16 In Java, instantiation means A) noticing the first time something is used B) creating a new object of the class C) creating a new alias to an existing object D) launching a method E) none of the above
Question 17 An alias when A) Two different reference variables refer to the same physical object B) Two different numeric variables refer to the same physical object C) Two different numeric variables contain identical values D) Two variables have the same names E) none of the above
Question 18. An API is A) an Abstract Programming Interface B) an Application Programmer s Interface C) an Application Programming Interface D) an Abstract Programmer s Interface E) an Absolute Programming Interface
Question 19. Complete the following assignment statement that generates a random number between 20 (inclusive) to 40 (inclusive).
Question 20 Given a String object Computer Science whose object reference is major, use the object reference to invoke proper String instance methods to create a substring containing Sci, calculate the length of the substring, display both the substring and its length on the same line. String major = new String( Computer Science ); String substringofmajor; //Store the substring of major int lengthofmajor; //Store the length of the substring
Question 20
Question 20 (Grading Guideline) 25 points 10 for correct substring call take off 5 points if the numbers are not correct 10 for correct call to length() 5 for correct output: the substring and it s length must be on the same line