Midterm%20-%202%20-%202008%20-%20solutions

Midterm%20-%202%20-%202008%20-%20solutions - MIDTERM...

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

View Full Document Right Arrow Icon
COMP 113 – Computer Programming I Computing Science Department Part 1 – Short Answer Questions [2 marks] 1. Write the method readInput() , that could be added after the main() method in an application file. It receives, from main(), the prompt to be printed when the method is run and it returns the user's line of input (not just a single word). [Use the Scanner class to obtain input.] Here is an example of the statement in main() that would call readInput() : String phrase = ""; phrase = readInput("What is your full name?"); public static String readInput(String prompt) { Scanner keyboard = new Scanner(System.in); return keyboard.NextLine(); } [2 marks] 2. Write a static method isEven that use the modulus operator (%) to determine if an integer is even. The method should take an integer argument and return true if the integer is even and false otherwise. Assume that the method is in class Compare. In addition, write a statement that could be used to call this method in an application program . public static boolean isEven (int entry) { if ((entry%2)==0) return true; else return false; } [1 mark] 3. Find the error(s) in each of the following methods: a) public int sum (int x, y) public int sum (int x, int y) { int result; result = x + y; return result; } b) public static void product(int a, double b ) public static void product( double a, int b ) { System.out.println(“Result is “ + a * b); } Note to b : The call statement in main( ) is: product( 3.5, 2 ); MIDTERM EXAMINATION II - SOLUTIONS
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
COMP113—Midterm II—Fall 2008 - Solutions [2 marks] 4. Scope questions. Determine the value displayed in the identified output statement(s). a)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Midterm%20-%202%20-%202008%20-%20solutions - MIDTERM...

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

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