Casual Discussion, Warm-up Questions, and Lecture Demo Exercises

advertisement
Lecture Exercise 01
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Casual Discussion, Warm-up Questions, and Lecture Demo Exercises
Q1. The code on the right is written in JAVA. Choose the correct answers below.
(System.out.println is just like cout in C++)
int i=10;
while (i>0) i‐‐; System.out.println(i); After the loop finishes, i is 0 / -1
The loop body runs 11 / 10 / 9 times
The condition (i>0) is checked 11 / 10 / 9 times
Q2. Which of the following is the best explanation for the assignment statement a=b; ?
 (a) Make a the same as b  (b) Change a's value by assigning b's value to a
 (c) Change b's value by assigning a's value to b
Q3. Given a Day class which has the instance fields year, month, day , which one below is a correct constructor?
public Day(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
public
{
int
int
int
}
Day(int y, int m, int d)
public
{
y =
m =
d =
}
year = y;
month = m;
day = d;
Q4. On the right is a java program.
The output from lines 3 and 4 are shown in the comments.
(i) Does the data type of i change after line 2? Yes / No
Line 1 Line 2 Line 3 Line 4
(ii) Which saying(s) is/are accurate concerning (char)i?
Day(int y, int m, int d)
year;
month;
day;
public static void main(String [] args) { int i=97; char c; c=(char)i; System.out.println(i); //output: 97 System.out.println(c); //output: a }  (a) (char)i means to convert i to char.  (b) (char)i means to convert the value of i to a char value.  (c) (char)i means to provide a char value by converting from the value of i.  (d) (char)i can be used if we want to consider the value of i as a char. Q5. The compiler has caught a problem for each of the following, at the given line numbers. What's wrong?
1 2 3 4 5 6 7
int sum = 0; for (int i=0; i<10; i++) sum = sum + i; System.out.println(sum);
System.out.println(i);
Line 7: i cannot be resolved to a variable 1 2 3 4 5 6 7
int m(int i) { return i*2; i++; } Line 4: unreachable code
1 2 3 4 5 6 7
int larger(int i, int j) { if (i>j) return i; if (i<=j) return j; } Line 1: This method must return a result of type int Q6. Why does the following program outputs 0.89999.. instead of 0.9?
public static void main(String[] args) { System.out.println(2.0‐1.1); //output 0.8999999999999999 }
Last modified: 15‐Jan‐2016 1
Lecture Exercise 01
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q7. Work out the output of the following program:
public static int f(int n) { System.out.println(n); if (n == 0 || n == 1) return 1; else return f(n‐1) + f(n‐2); } public static void main(String[] args)
{ System.out.println( "Final result: "+f(5)); } Output: Final result: Q8. Redo Lec01 Ability Test: Write a C++ function, search_section, which counts a value in a section of an array.
Example of using search_section:
void main()
{
const int arr[ ]={1,3,5,7,9,2,4,7,8,7};
cout << "Within arr[2..8], the value 7 occurs: ";
cout << search_section(arr, 2, 8, 7);
cout << " times";
}
Count 7 from arr[2] to arr[8]
_______ search_section ( const int A[ ], ____________________)
{
while(
{
)
}
}
Last modified: 15‐Jan‐2016 2
Download