CS1102 Review Quiz 4 PDF

Title CS1102 Review Quiz 4
Course Programming 1
Institution University of the People
Pages 28
File Size 715.4 KB
File Type PDF
Total Views 147

Summary

 Home  My courses  CS 1102 - AY2020-T  Final Exam (Days 1 - 4)  Review QuizStarted on Sunday, 16 August 2020, 5:33 PMState FinishedCompleted on Sunday, 16 August 2020, 6:12 PMTime taken 38 mins 17 secsMarks 8/35.Grade 22 out of 100.Question  1Incorrect Mark 0 out of 1.Flag question Qu...


Description

    

Home My courses CS 1102 - AY2020-T5 Final Exam (Days 1 - 4) Review Quiz

Started on

Sunday, 16 August 2020, 5:33 PM

State Finished Completed on Time taken Marks

Sunday, 16 August 2020, 6:12 PM 38 mins 17 secs 8.00/35.00

Grade 22.86 out of 100.00

Question 1 Incorrect Mark 0.00 out of 1.00

Flag question Question text

What is the output of the following Java program? import java.util.*; class ArrayGames { public static void main(String[] args) { int[] a = {1,2,3,4,5}; for (int c : a) c *= c; System.out.println(Arrays.toString(a)); } } Select one: a. [0, 1, 2, 3, 4] b. [1, 1, 1, 1, 1]

c. [1, 2, 3, 4, 5] d. [1, 4, 9, 16, 25]

e. No output. It throws an exception. Feedback Your answer is incorrect. The for-each loop appears to square each element, but it really just squares a copy of each element. The array values are not changed. See Section 7.1.1. The correct answer is: [1, 2, 3, 4, 5]

Question 2 Correct Mark 1.00 out of 1.00

Flag question Question text

Consider the following Java program: 1 public class HelloWorld { 2 // My first program! 3 public static void main(String[] args) { 4 System.out.println("Hello, World!"); 5 } 6 } What is on line 1? Select one: a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition

Feedback Your answer is correct. See Section 2.1 of Eck (2014).

The correct answers are: a variable declaration, a class definition

Question 3 Correct Mark 1.00 out of 1.00

Flag question Question text

Which one of the following lines is a Java method call? Select one: a. import javax.swing.JOptionPane; b. int x = 0; c. compute();

d. return y; e. void compute() {} Feedback Your answer is correct. See Section 4.2.2 of Eck (2014). The correct answer is: compute();

Question 4 Incorrect Mark 0.00 out of 1.00

Flag question Question text

Consider the following Java program. Which object registers event listeners? import java.awt.event.*; import javax.swing.*; public class MouseWhisperer extends JFrame implements MouseListener { MouseWhisperer() {

super("COME CLOSER"); setSize(300,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addMouseListener(this); setVisible(true); } public public public public

void void void void

mouseClicked(MouseEvent e) { setTitle("OUCH"); } mousePressed(MouseEvent e) { setTitle("LET GO"); } mouseReleased(MouseEvent e) { setTitle("WHEW"); } mouseEntered(MouseEvent e) { setTitle("I SEE YOU");

} public void mouseExited(MouseEvent e) { setTitle("COME CLOSER"); } public static void main(String[] args) { new MouseWhisperer(); } } Select one: a. java.awt.event b. JFrame

c. MouseEvent d. MouseListener e. this Feedback Your answer is incorrect. "this" is registered as a listener by the "addMouseListener" call. (Also, "this" is the only choice that is an object. The other choices are packages, classes, or interfaces.) See Section 6.3.1. The correct answer is: this

Question 5 Incorrect Mark 0.00 out of 1.00

Flag question Question text

Consider the following line of Java code. System.out.println("Hello, World!"); The full line of code is which of the following?

Select one: a. a class b. a method (subroutine) c. an object d. a parameter

e. a statement Feedback Your answer is incorrect. See Section 2.1 of Eck (2014). The correct answer is: a statement

Question 6 Correct Mark 1.00 out of 1.00

Flag question Question text

Consider the following Java program. Which statement registers an object to receive events? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener { int count; JButton button; Clicker() { super("Click Me"); button = new JButton(String.valueOf(count)); add(button); button.addActionListener(this); setSize(200,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { count++; button.setText(String.valueOf(count)); }

public static void main(String[] args) { new Clicker(); } } Select one: a. add(button); b. button.addActionListener(this);

c. button.setText(String.valueOf(count)); d. setVisible(true); e. super("Click Me"); Feedback Your answer is correct. See Section 6.1.3. The correct answer is: button.addActionListener(this);

Question 7 Correct Mark 1.00 out of 1.00

Flag question Question text

Where in a Java file can the text "int x" NOT appear? Select one: a. in a class definition but outside a method definition b. in a for statement c. in a method call

d. in a method definition e. in a method parameter list Feedback Your answer is correct. Method calls have variable names, values, and expressions. The do not have type identifiers. See Sections 4.2.2, 4.2.4, 4.3.2, and 4.7.1 of Eck (2014). The correct answer is: in a method call

Question 8 Incorrect Mark 0.00 out of 1.00

Flag question Question text

What is the output of the following Java program? import java.util.*; class ArrayGames { public static void main(String[] args) { Integer[] a = new Integer[5]; System.out.println(Arrays.toString(a)); } } Select one: a. null b. [0, 0, 0, 0, 0] c. [5, 5, 5, 5, 5] d. [null, null, null, null, null] e. No output. It throws an exception.

Feedback Your answer is incorrect. The elements of "a" get the default initialization, which is "null" for object references. Recall that "Integer" is a class type, not a primitive type like "int". See Sections 4.2.4, 5.1.2, and 7.3.2. The correct answer is: [null, null, null, null, null]

Question 9 Correct Mark 1.00 out of 1.00

Flag question Question text

Consider the following Java program. What is the superclass of "Clicker"? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener { int count; JButton button; Clicker() { super("Click Me"); button = new JButton(String.valueOf(count)); add(button); button.addActionListener(this); setSize(200,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { count++; button.setText(String.valueOf(count)); } public static void main(String[] args) { new Clicker(); } } Select one: a. ActionEvent b. ActionListener c. JButton d. JFrame

e. this Feedback Your answer is correct. See Section 5.5.2. The correct answer is: JFrame

Question 10 Correct Mark 1.00 out of 1.00

Flag question Question text

Consider the following line of Java code. System.out.println("Hello, World!"); Which one of the following does NOT describe '"Hello, World!"'? Select one: a. a declaration

b. an expression c. a literal d. a parameter e. a statement Feedback Your answer is correct. '"Hello, World!"' is a String literal, which means it is also a String expression. It is the parameter to the method "println". It is not a declaration statement. See Sections 2.2.4 and 2.2.5 of Eck (2014).

The correct answer is: a declaration

Question 11 Correct Mark 1.00 out of 1.00

Flag question Question text

What is output by the following Java program? class Zap { static boolean zap() { return true; } static int zap(boolean x) { return 0; } static double zap(int x) { return 0.5; } static String zap(double x) { return "Zap!"; } static boolean zap(String x) { return false; } public static void main(String[] args) { System.out.println(zap(zap(zap(zap())))); } } Select one:

a. true b. 0 c. 0.5 d. Zap!

e. false Feedback Your answer is correct. The innermost call to "zap" has no argument, so it uses the first method definition, which returns the boolean value true. The next call to "zap" thus gets a boolean argument, so it uses the second method definition, which returns the int 0. The next call to "zap" gets this int argument, so it uses the third method definition, which returns the double 0.0. The outmost call to "zap" thus gets a double argument, so it uses the fourth method definition, which returns the String "Zap!" See Section 4.3.3 of Eck (2014). The correct answer is: Zap!

Question 12 Correct Mark 1.00 out of 1.00

Flag question Question text

What is the output of the following Java program? public class Food { static int count; private String flavor = "sweet"; Food() { count++; } void setFlavor(String s) { flavor = s; } String getFlavor() { return flavor; } static public void main(String[] args) { Food pepper = new Food(); pepper.setFlavor("spicy"); System.out.println(pepper.getFlavor()); } } Select one: a. 1

b. 2 c. spicy

d. sweet e. The program does not compile. Feedback Your answer is correct. See Section 5.1.3 of Eck (2014). The correct answer is: spicy

Question 13 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? import java.util.*; class ArrayGames { public static void main(String[] args) { int[] a = {1,2,3,4,5}; System.out.println(a[a[1]]); } } Select one: a. 1 b. 2 c. 3 d. 4 e. 5 Feedback Your answer is incorrect. Array indices start at 0, so "a[1]" is 2, which leads to "a[2]", which is 3. See Section 7.1. The correct answer is: 3

Question 14 Not answered Marked out of 1.00

Flag question Question text

Consider the following line of Java code. System.out.println("Hello, World!"); "System" is which of the following? Select one: a. a class b. a method (subroutine) c. an object d. a parameter e. a statement Feedback Your answer is incorrect. "System" is a built-in Java class with static member objects, like "out". See Section 2.3.1 of Eck (2014). The correct answer is: a class

Question 15 Not answered Marked out of 1.00

Flag question Question text

In a for loop, how many times does the continuation condition run? Select one: a. At least once, at the beginning of each iteration. b. At least once, at the end of each iteration. c. Exactly once.

d. Zero or more times, at the beginning of each iteration. e. Zero or more times, at the end of each iteration. Feedback Your answer is incorrect. See Section 3.4.1 of Eck (2014). The correct answer is: At least once, at the beginning of each iteration.

Question 16 Not answered Marked out of 1.00

Flag question Question text

Consider the following line of Java code. System.out.println("Hello, World!"); The full line of code is which of the following? Select one: a. a class b. a method (subroutine) c. an object d. a parameter e. a statement Feedback Your answer is incorrect. See Section 2.1 of Eck (2014). The correct answer is: a statement

Question 17 Not answered Marked out of 1.00

Flag question Question text

What is output by the following Java program? Zap { static boolean zap() { return true; } static int zap(boolean x) { return 0; } static double zap(int x) { return 0.5; } static String zap(double x) { return "Zap!"; } static boolean zap(String x) { return false; } public static void main(String[] args) { System.out.println(zap(zap(zap(zap(1))))); } } Select one: a. true b. 0 c. 0.5 d. Zap! e. false Feedback Your answer is incorrect. The innermost call to "zap" has argument 1, which is an int, so it uses the third method definition, which returns the double 0.5. The next "zap" call then gets this double as an argument, so it uses the fourth method definition, which returns the String "Zap!" So the next call gets a String argument, which means it uses the fifth method definition, which returns the boolean false. The outermost "zap" call gets this boolean, so it uses the second definition and returns the int 0. The first definition of "zap" is never used. See Section 4.3.3 of Eck (2014). The correct answer is: 0

Question 18 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? class Food { String flavor = "bland"; void printFlavor() { System.out.println(flavor); } } class Pepper extends Food {

String flavor = "spicy"; void printFlavor() { System.out.println(flavor); } } public class Lunch { public static void main(String[] args) { Food lunch = new Pepper(); lunch.printFlavor(); } } Select one: a. bland b. bland spicy c. no output d. spicy e. the program does not compile Feedback Your answer is incorrect. The "printFlavor" methods in "Food" and "Pepper" look identical, but they refer to different "flavor" member variables. Because of polymorphism, the "Pepper" override of "printFlavor" gets called, which refers to the "flavor" member declared by "Pepper". See Sections 5.5.4 and 5.6.1. The correct answer is: spicy

Question 19 Not answered Marked out of 1.00

Flag question Question text

Consider the following Java statements. int x = 3; x = x++; What is the value of x after these statements are executed? Select one: a. 0 b. 3 c. 4

d. 5 e. The question is moot. The statements have a syntax error. Feedback Your answer is incorrect. Though "x++" increments the value of "x" from 3 to 4, it returns the previous value of "3" as an expression. That value, 3, then gets assigned back to "x" with the "x =" assignment. In the end, "x" has the value it started with. See Section 2.5.2 of Eck (2014). The correct answer is: 3

Question 20 Not answered Marked out of 1.00

Flag question Question text

In a for loop, how many times does the initialization run? Select one: a. At least once, at the beginning of each iteration. b. At least once, at the end of each iteration. c. Exactly once. d. Zero or more times, at the beginning of each iteration. e. Zero or more times, at the end of each iteration. Feedback Your answer is incorrect. See Section 3.4.1 of Eck (2014). The correct answer is: Exactly once.

Question 21 Not answered Marked out of 1.00

Flag question Question text

In a do-while loop, how many times does the continuation condition run (if the loop has no break, return, or System.exit calls)? Select one: a. At least once, at the beginning of each iteration. b. At least once, at the end of each iteration. c. Exactly once. d. Zero or more times, at the beginning of each iteration. e. Zero or more times, at the end of each iteration. Feedback Your answer is incorrect. See Section 3.3.2 of Eck (2014). The correct answer is: At least once, at the end of each iteration.

Question 22 Not answered Marked out of 1.00

Flag question Question text

Which one of the following is used in Java programming to handle asynchronous events? Select one: a. event handlers b. pragmatics c. protocols d. reserved words e. short circuits Feedback Your answer is incorrect. See Section 1.2 of Eck (2014). The correct answer is: event handlers

Question 23 Not answered Marked out of 1.00

Flag question Question text

Which of the following is NOT a valid identifier in Java? Select one: a. p b. Public c. public d. public23 e. PuBlIc_tWeNtY_3 Feedback Your answer is incorrect. "public" is a reserved word in Java and cannot be used as an identifier. See Section 2.2 of Eck (2014). The correct answer is: public

Question 24 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? public class Food { static int count; private String flavor = "sweet"; Food(String s) { flavor = s; } void setFlavor(String s) { flavor = s; } String getFlavor() { return flavor; } static public void main(String[] args) { Food pepper = new Food(); System.out.println(pepper.getFlavor());

} } Select one: a. 1 b. 2 c. spicy d. sweet e. The program does not compile. Feedback Your answer is incorrect. The program defines a constructor that takes parameters, but it tries to call a constructor without parameters. If a class has an explicit constructor, the Java compiler does not provide a default one. See Section 5.2.2 of Eck (2014). The correct answer is: The program does not compile.

Question 25 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? class Food { String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; } public class Lunch { public static void main(String[] args) { Pepper lunch = new Pepper(); System.out.println(lunch.flavor); } } Select one: a. bland

b. bland spicy c. no output d. spicy e. the program does not compile Feedback Your answer is incorrect. The member "flavor" of class Pepper hides the member "flavor" of class Food. A variable of type "Pepper" sees the "Pepper" member by default. See Section 5.6.2. The correct answer is: spicy

Question 26 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? import java.util.*; class ArrayGames { public static void main(String[] args) { int[] a = {1,2,3,4,5}; System.out.println(a[4]); } } Select one: a. 1 b. 2 c. 3 d. 4 e. 5 Feedback Your answer is incorrect. Recall that array indices start at 0, so index 4 is the 5th element. See Section 7.1. The correct answer is: 5

Question 27 Not answered Marked out of 1.00

Flag question Question text

Consider the following Java program. Which statement displays a window with a button on the screen? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener { int count; JButton button; Clicker() { super("Click Me"); button = new JButton(String.valueOf(count)); add(button); button.addActionListener(this); setSize(200,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { count++; button.setText(String.valueOf(count)); } public static void main(String[] args) { new Clicker(); } } Select one: a. add(button); b. button.addActionListener(this); c. button.setText(String.valueOf(count)); d. setVisible(true); e. super("Click Me"); Feedback Your answer is incorrect. See Section 6.1.1. The correct answer is: setVisible(true);

Question 28 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? class Food { void flavor() { System.out.println("bland"); } } class Pepper extends Food { void flavor() { System.out.println("spicy"); } } public class Lunch { public static void main(String[] args) { Food lunch = new Pepper(); lunch.flavor(); } } Select one: a. bland b. bland spicy c. no output d. spicy e. the program does not compile Feedback Your answer is incorrect. The method "flavor" in class "Pepper" overrides the one in class "Food". Because of polymorphism, the variable "lunch" uses the "Pepper" method even though it is type "Food". This is because it refers to an object of type "Pepper". See Section 5.5.4. The correct answer is: spicy

Question 29 Not answered Marked out of 1.00

Flag question Question text

Which one of the following Java technologies eliminates memory leaks? Select one: a. garbage collection b. just-in-time compilers c. object-oriented analysis and design d. software engineering e. virtual machines Feedback Your answer is incorrect. See Section 5.2.3 of Eck (2014). The correct answer is: garbage collection

Question 30 Not answered Marked out of 1.00

Flag question Question text

What is the output of the following Java program? class Food { Food() { System.out.println("bland"); } } class Pepper extends Food { Pepper() { this("spicy"); }...


Similar Free PDFs