=========================preview======================
(COMP1022P)[2012](f)midterm~=4ow7^_55812.pdf
Back to COMP1022P Login to download
======================================================
The Hong Kong University of Science and Technology
COMP1022P: Introduction to Computing with Java
Midterm Examination
Fall 2012
October 26, 2012
20:00 C 22:00 (2 hours)
Student Name: Lecture Section:
Student ID: Lab Section:
Instructions:
1.
This is an open-book and open-notes examination.
2.
Check that you have all 18 pages.
3.
Write your name, student ID, lecture section, and lab section on this page.
4.
Answer all questions in the space provided.
5.
The last page is for rough works and it can be detached from the exam paper
Question Score
1-20 MC Questions /40
21 Variables and Scope /10
22 User-defined methods /10
23 User-defined methods /10
24 Branching Statements /10
25 If-else statements /10
26 Loops /10
Total /100
Q1. MC Questions (Total 40 marks, 2 marks each)
Answer the following questions in the space provided. 2 points for each correct answer.
1. Which of the following is a valid data type?
A. integer
B. floating
C. boolean
D. number
2. Which of the following statement is legal in Java?
A. byte abc =500;
B. boolean d = TRUE;
C. int i = "9999";
D. double x = 1.23;
3. What is the value of variable z after executing the following statements?
int x = 1;
int y = x++;
int z = ++y;
A. 0
B. 1
C. 2
D. 3
4. Let variable a be initialized as follows:
final int a = 5;
Which of the following statement(s) is/are legal in Java?
1) a = 30;
2) System.out.println(a);
3) int b = a;
4) a = a + 10;
5) String b = a;
A. (1) and (2)
B. (2) and (3)
C. (3) and (5)
D. (2) only
5. Which of the following statement is true?
A. A local variable is destroyed and its memory is released when the program execution exits its scope.
B. When a variable or a method is declared using Public identifier, it can be visible to any class in any package.
C. Subclasses inherit properties and methods from the superclass.
D. All of the above statements are true.
6. Which of the following is the major use of a Constructor?
A. It is to avoid bugs
B. It is to initialize an object when it is created
C. It is to improve the program execution speed
D. It is to make the use of computer memory more efficient
7. Given the following program:
class Temp{
int a = 5, b =7;
public Temp(){
a = a + 2;
if(a!=b)
System.out.println("a: "+a+", b: "+b);
else
System.out.println("They are the same");
}
}
When a Temp object is created, what will it output?
A. They are the same
B. a: 5, b: 7
C. a: 7, b: 7
D. a: 7, b: 5
8. Which of the following statement is legal in Java?
A. int double;
B. int double1;
C. int catch-22;
D. int 1_or_2;
9. Given the following program:
class Temp{
int a = 5, b =20;
public int add(int a, int b){
return a + b;
}
public Temp(){
int b =2;
b = add(a,b);
System.out.println("a: "+a+", b: "+b);
}
}
When a Temp object is created, what will i