This document contains a 20 question midterm exam for an intro to computer science course. The questions cover topics like Java keywords, data types, control flow, classes, methods, and more. Students are instructed to show their work in the spaces provided and not separate pages. The exam is closed book with no external aids permitted.
This document contains a 20 question midterm exam for an intro to computer science course. The questions cover topics like Java keywords, data types, control flow, classes, methods, and more. Students are instructed to show their work in the spaces provided and not separate pages. The exam is closed book with no external aids permitted.
This document contains a 20 question midterm exam for an intro to computer science course. The questions cover topics like Java keywords, data types, control flow, classes, methods, and more. Students are instructed to show their work in the spaces provided and not separate pages. The exam is closed book with no external aids permitted.
Midterm Test, Summer 2012 80 Minutes Name: Student Number: CSE email: Note: Complete all questions in the space provided. Do not separate pages. Ensure your name and student number are on all pages. This is a closed-book test. No external aids are permitted. DO NOT OPEN THE BOOKLET UNTIL INSTRUCTED TO DO SO NAME: NUMBER: 1/10 1. [1 mark] What is the value of x after execution of the Java program fragment int x = 8 + (4 - 3) / 2 - 2 * 3 % 4; 2. [1 mark] Identify the package, subpackage and Class in the Java statement import java.lang.System, 3. [2 marks] Circle all of the keywords in the following Java program fragment import java.lang.System; public class Blue { public static void main(String[] args) { String s = Hello + + world; int x = 2; System.out.println(s); } } 4. [1 mark] What does the following Java program fragment output? If it does not output anything, explain why. int i = 15; long l = 2; NAME: NUMBER: 2/10 oat f = i / l; double d = f * 2 / 14; System.out.println(d);
5. [2 marks] What does the following Java program fragment output? If it does not output anything, explain why. char c1 = g + 1; char c2 = (f-a) / 2 - 2 + a; char c3 = l; System.out.printf(%c%c%c%n,c1,c2,c3); 6. [2 marks] In the following Java program fragment, rewrite the following assignment statements with the castings necessary for the code to compile. oat x = (oat)3.0; long j = 2; double x = 7; int y = (int) 2L; double x = 3.0f; NAME: NUMBER: 3/10 7. [2 marks] Draw the UML diagram for a class Blue which has the following public properties A public eld hue which is an int. A public method darken which takes a oat as a parameter and returns an int 8. [2 marks] Draw the UML diagram for the class Blue and two instances of Blue (azure and navy). azures hue is 4 and navys hue is 11. 9. [1 mark] The Integer class contains a eld MAX_VALUE that represents the maximum values that an int can have. What happens when the Java fragment int x = Integer.MAX_VALUE+1; is executed? What value does x have? NAME: NUMBER: 4/10 10. [2 marks] Consider the following Java program fragment.
Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); Suppose that the input consists of 123 2 3 45 What values are x, y and z set to? If the fragment will generate a runtime error on the input, describe why. 11. [2 marks] In Java, what is autoboxing? 12. [2 marks] What does the following Java program fragment output? Fraction f = new Fraction(2,11); Fraction g = f; Fraction h = new Fraction(2,11); NAME: NUMBER: 5/10 f.setNumerator(3); g.setNumerator(4); h.setNumerator(5); System.out.printf(%d %d %d%n, f.getNumerator(),g.getNumerator(),h.getNumerator()); 13. [2 marks] What does the following Java program fragment output? Fraction f = new Fraction(2,11); Fraction g = f; Fraction h = new Fraction(2,11); System.out.printf(%b %b %b %n,f == g, f == h, g == h); System.out.printf(%b %b %b %n, f.equals(g), f.equals(h), g.equals(h)); 14. [2 marks] Does every instance of an object have a toString() method? Why or why not? 15. [2 marks] What does the following Java program fragment output? int x = 3; int y = 3; if(x == 3) NAME: NUMBER: 6/10 if(y == 4) System.out.println(A); else System.out.println(B); System.out.println(C); 16. [1 mark] What does the following Java program fragment output? for(int i=0;i<5;i++); System.out.println(i); 17. [2 marks] What does the following Java program fragment output? int i = 0; for(int j=0;i >= j; j++) { i = i + 1; if(i == 3) i = 0; System.out.println(j); } NAME: NUMBER: 7/10 18. [2 marks] Trace this Java program fragment. What does it output? for(int i=0;i<5;i++) { for(int j=i;j<=5-i;j++) { System.out.println(j); } } 19. [2 marks] What does the following program fragment output? String s = madamimadam; boolean res = true; for(int i=0;i<s.length()/2;i++) { boolean v = (s.charAt(i) == s.charAt(s.length()-i-1); res = res & v; NAME: NUMBER: 8/10 System.out.println(v); } System.out.println(res); 20. [2 marks] What does the following program fragment output? public class T { public static void main(String[] args) { String s = "all this"; boolean x = false; boolean y = false; int z = 0; s = s + s; for(int i=0;i<s.length();i++) { x = s.charAt(i) == 'a'; y = s.charAt(i) > 'r' || y; if(x||y) z++; } System.out.printf("%b %b %d%n",x,y,z); } } NAME: NUMBER: 9/10 NAME: NUMBER: 10/10