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

CS100J, Fall 2005 Answers to Sample Prelim 1 Questions Sample questions Below, we give some sample questions. The answers are given after all the questions. Note that you may be asked to write a small procedure or function, using assignments, if-statements, blocks, and return statements. Two sample questions of this nature appear at the end of the sample questions. 1. (a) When do you use "=" and "==" (b) what is the difference between 'c' and "c" (c) if b == true and c ==" true " what is the type of the variables b and c? (d) What is the difference between a method declared with keyword static and one without the keyword? 2. Below is a class. Draw a folder of this class. public class A{ public static void main(){ int a= 2 ; int b= negate(negate(a)) ; } public static int negate( int x) { return (–x) ; } } 3. Find the various syntax and semantic errors in the code given below: public class A{ public static void main() { String a= "true" String b= false ; int d = diffinlength(a) } public static boolean diffinlength(String s1, String s2) { return (abs(s1.length – s.length())) } } 4. Starting with values a=5 b=23 c=7 d=0 b1= true b2= false Find the values of a, b, c, d. Start with the above values for EACH item. (a) if ((a%a)==d) (b) d= b/c ; d= 3; a= a–d ; (c) a= a*5 ; (d) if (b1) b2= true ; a= a – –5 if (b2) b1= false ; (e) if (b1 || (a!=3)) b2= true 5. Give the syntax of the assignment statement and write down how to execute it. 6. Give the syntax of a block and explain how to execute it. 7. Below is a class Employee. public class Employee {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
private String name; // employee’s name private Date hireDate; // date employee was hired /** Constructor: employee named n hired on date d public Employee (String n, Date d) { … } /** = name of the employee */ public String getName() { …} /** = hireDate */ public Date getHireDate() {…} /** = a representation of the employee, giving their name and date of hire *./ public String toString() { …} } (a) Write the three method bodies (but not the constructor body). (b) Write a new-expression to create an Employee with name “Roger” who is hired at the time the new- expression is evaluated. Draw the manila folder that represents the newly created object. 8. Look at class Employee of the previous exercise. (a) Write a subclass VIP that has a field, bonus, which contains a double value. The subclass needs a constructor that initializes all three fields. Make sure you write the body of the constructor correctly. The subclass should have its own toString function and a getter method for the bonus. (b) Which components does subclass VIP inherit? Which does it override?
This is the end of the preview. Sign up to access the rest of the document.