NYU COMS W1007 - Practice Midterm Solutions

Unformatted text preview:

S1007 - Introduction to Computer SciencePractice Midterm SolutionsSummer 2003Name:CUNIX ID:You have ninety minutes to answer all of the questions below. Write your answers in thespace provided. You should read over the entire exam before you answer any questions andbudget your time accordingly.Question Score Possible1. 82. 103. 104. 45. 46. 4Total 401. (8 points) Describe an algorithm for finding the second-largest number in a list ofintegers. The n-item list is contained in a sequence of variables X0, X1, . . . , Xn. Do notworry about array references; assume you can access any item Xidirectly by name. Theresult should be placed in a variable Y .The minimum possible value of an integer iscontained in a variable MIN INT. Each step of your algorithm should be precisely defined,but it need not be valid Java.1. max := X0, Y := X0, i := 1.2. If Xi> max, Y := max, max := Xi, go to Step 4.3. If Xi> Y , Y := Xi.4. i := i + 1.5. If i ≤ n, go to Step 2.6. Return result Y .2. (10 points) Find 10 errors (there are more than 10) in the following Java code. Theerrors are both syntactic (compiler errors) and logical (bugs). For each error, identify theline number and briefly explain how to fix it.1 private class Circle {2 public static final float PI = 3.14 ;34 private double x, y ; /* The center of the circle. */5 private double r ; /* The radius of the circle. */67 public Circle(double r) {8 this(0.0,0.0,this.r) ;9 }1011 public double Circle(double x, double y, double r) {12 this.x = x ;13 this.y = y ;14 this.r = r ;1516 /* Returns the area of the circle. */17 public double area() {18 return PI * r^2 ;19 }2021 /* Indicates whether a point is on the boundary of the circle.22 public boolean onCircle(double x1, y1) {23 int dx = x1 - x ;24 int dy = y1 - y ;25 return dx*dx + dy*dy == r*r ;26 }2728 /* Creates a series of concentric circles */29 public static void main(string[] args) {30 int i ;3132 for( int i=0 ; i < 10 ; i++ )33 Circle c = Circle(10.0*i) ;34 System.out.println("r="+c.r+" A="+c.area) ;35 }36 }2. (cont’d)Line 1: private is not a valid class modifier. Removing it would give theclass package scope.Line 2: 3.14 is not a float literal. Change it to 3.14F.Line 8: this.r refers to the field r. The parameter in the call to thisshould just be plain r.Line 11: Constructors may not have a return type. Omit double.Line 15: There is no closing brace on the constructor.Line 18: r^2 is not “r squared”. “^” is the XOR operator. Since r isn’teven an integer, the operator is invalid. The expression should be PI * r* r.Line 21: There is no “*/” closing the comment.Line 22: There is no type specifier on y1. It should presumably be adouble.Lines 23-24: int variables dx and dy are assigned the value of a floating-point operation without a type cast. The variables should be of typedouble.Line 25: As written this is an equality comparison between an integerexpression and a floating-point expression. Even changing dx and dy todouble, as above, testing floating-point results for equality is unlikely toproduce the desired results, because of rounding errors. The value shouldbe tested against some threshold of error, e.g.:public boolean onCircle(double x1, double y1) {static final double EPSILON = 0.01 ;double dx = x1 - x ;double dy = y1 - y ;return (dx*dx + dy*dy - r*r) < EPSILON ;}Line 29: string should be String.Line 30-32: int i is defined twice inside of main.Line 32-34: There are no braces surrounding the for loop. As is, theprintln statement will only print the value of the last Circle.Line 34: The call to area does not have parentheses.3. (10 points) Write a class named IntBits with a single private field named num of typeint. The methods of your class should be as follows:• A public constructor that takes a single integer parameter and uses it to initialize num.• A public method named printBits that prints out each of the significant bits in num(zero bits to the left of the most significant non-zero bit should be ignored).printBits takes no parameters and has return type void. The output should looksomething like: “0=0 1=0 2=1 3=0 4=1, etc.”.• A main method that creates ten instances of IntBits with the parameters10,20,30,. . . ,100 and invokes printBits on each one. (Warning: points will be takenoff if main contains more than five lines of code.) (Hint: Use a loop.)public class IntBits {private int num ;public IntBits(int num) {this.num = num ;}public void printBits() {int i = 0 ;if( num==0 )System.out.println("0=0") ;else {int n = num ;while( n != 0 ) {System.out.print(i+"="+(n&1)+" ") ;n >>>= 1 ;i++ ;}}}public static void main(String[] args) {for(int i=10 ; i <= 100 ; i+=10) {IntBits ibits = new IntBits(i) ;System.out.println("The bits of "+i+":) ;i.printBits() ;}}4. (4 points) What is the difference between a public and private member of a Java class?A public member of a class can be read and modified by any class. Aprivate member of a class can only be red and modified within the classitself.5. (4 points) What are the values of x and y after the following code executes?int x = 0 ;int y = x++ + ++x ;x++ has higher precedence than ++x, so it is evaluated first. The value ofx++ is 0 and x becomes 1. ++x increments x and returns the new value 2.x = 2y = 0+2 = 26. Compute the value of each of the following Java expressions (2 points each):a) 36/(23>>2)*((1<<2)+1) + (9<<2)%523>>2 = 0001 01112>> 2= 0000 01012= 5(1<<2)+1 = ( 00012<< 2) + 1= 01002+ 1= 01012= 59<<2 = 0000 10012<< 2= 0010 01002= 3636/5*5 + 36%5 = 36b) ((byte)(127+1) > 0) && (true == false) || (21.0/4.0 > 5)(byte)(127+1) > 0 = -128 > 0= falsetrue == false = false21.0/4.0 > 5 = 5.25 > 5= truefalse && false || true = (false && false) || true=


View Full Document

NYU COMS W1007 - Practice Midterm Solutions

Download Practice Midterm Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Practice Midterm Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Practice Midterm Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?