Faculty of Computing and Information Technology - Rabigh
Information Technology Department King Abdulaziz University
MIDTERM EXAMINATION I SEMESTER I [1430/31H]
SUBJECT CODE : CPCS 202
SUBJECT NAME : PROGRAMMING I
DATE : 15th NOVEMBER 2009
TIME : 2 HOURS
VENUE : MAIN BUILDING, KING ABDULAZIZ UNIVERSITY – RABIGH
Instructions to Candidates:
There are three sections in this paper. Answer all questions.
THIS PAPER CONSISTED OF SEVEN (7) PAGES INCLUDING THIS PAGE.
PART A (40 Marks) – 20 questions
1. What is the latest version the Java SE Development Kit?
A. JDK 1.1 C. SDK 1.5
B. SDK 1.2 D. SDK 1.6
2. A Java program can be executed using the following command.
A. java C. cmd
B. javac D. run
3. class Main
{
public static void main( String [] args )
{
String mesg = "Answer is ";
int sum = 1 + 2;
System.out.println( mesg + sum );
}
}
What is the effect of executing the class Main above (java Main).
A. Prints 3 C. Prints Answer is 1 + 2
B. Prints Answer is 3 D. Prints mesg + sum
4. Which assignments are illegal?
A. float f = -412; C. double d = 0x12345678;
B. int other = (int)true; D. short s = 10;
5. What is the parameter specification for the public static void main method?
A. string args [] C. String [] args
B. Strings args [] D. String args
6. The only way to ensure that a WHILE loop both starts and terminates is to A. use a loop condition which is
always true
C. use a loop condition which is always false
B. put code in the loop which will eventually make the loop condition false
D. put code in the loop which will eventually make the loop condition true
7. After the following code has executed
int x, y, z, result;
x=7; y=5; z=3; result= x + (y%z + 3) * x;
the value of result would be
A. 42 C. 94
B. 35 D. 12
8. Which of the following are syntactically correct Java statements?
A. if (x == y) then y = z; C. if(x==y)y=z;
B. do x++; while (x < y) D. while (a < b) a = b
9. Which of the following are invalid name for Java variables?
A. Ten_10 C. 77temperature
B. dog D. abc15
10. Which of the following are invalid primitive types in Java?
A. int C. float
B. boolean D. scientific
11. What range of values can be stored in the primitive type byte?
A. 0 to 255 C. -127 to 128
B. -128 to 127 D. -256 to 255
12. What is the value of the expression 11/2
A. 5.0 C. 5.5
B. 5 D. 6.0
13. What is the value of the expression (double) (11/2)
A. 5.0 C. 5.5
B. 5 D. 6.0
14. The negation or NOT operator in Java is represented by:
A. ! C. ||
B. && D. None of the above.
15. How many bits are there in 3 bytes?
A. 3 C. 24
B. 12 D. 32
16. What is the value of the following expression?
100 % 24 / 3
A. 1.3333333333333333 C. 2
B. 0 D. 1
17. What is the output of the following code segment?
int n = 8;
for (int i=0; i<n; i++) { System.out.print(i*(i-1)/2);
System.out.print(" ");
}
System.out.println();
A. 0 0 0 3 4 10 12 21 C. 0 0 1 3 6 10 15 21 B. 0 1 3 6 10 15 21 28 D. 0 3 4 10 12 21 24 36
18. Which statement will you use for testing whether the character variable ch is a single digit.
A. if ( (ch >= ’0’) || (ch <=’9’) ) C. if ( (ch >= ’0’) && (ch <=’9’) ) B. if ( (ch >= 0) && (ch <=9) ) D. if ( (ch % 10) > 0 )
19. public class Main
{
public static void main( String [] args)
{
int this_number = 3;
int that_number;
while ( this_number < 10 )
{
that_number = this_number;
this_number = this_number + that_number / 2;
}
System.out.println( "Answer is " + this_number );
}
}
What is the effect of executing the class Main above (java Main) A. Prints Answer is 12 C. Prints Answer is 14 B. Prints Answer is 13 D. Prints Answer is 15
20. What is the keyword used to stop and exit from a loop construct?
A. stop C. halt
B. break D. quit
PART B (30 Marks) – 15 questions
1. What is the instruction to compile a java program?
javac
2. The main() method has to be declared as public static void main (String [] args)
3. True or False: A Java program written for Windows needs to be recompiled to run it on Linux or Unix.
False
4. What is the term used to describe a compiled Java code.
Bytecode
5. What is the type of the variable used in the switch case selection?
Integer
6. A boolean in Java can only be assigned to which two special values?
True and False
7. What is the instruction used to write Java’s output to the screen.
System.out.println() and other variations
8. True or False: An identifier must begin with a letter, underscore, or dollar sign.
True
9. If x is an int equal to 25, what will x be after the statement x = x/4?
6
10. What are variables are used for?
Variables are used to store data.
11. How many ways are there to declare comments in Java?
Three ways ( //, /* … */, /// )
12. What are the if and if/else statements used for?
Alter the flow of the program or making decisions
13. Name the three control structures in Java used for repetition.
While, do-while, for
14. True or False: (2 + 2 = 4) && (5 – 3 = 1).
False
15. True or False: (2 + 2 = 4) || (5 – 3 = 1).
True
PART C (30 Marks) – 1 question
1. Write a java program to compare three numbers and to print out the largest number. The numbers must be entered from the command line. Make sure that your program is clearly written with the appropriate comments.
public class Largest {
public static void main(String[] args) { int largest = 0;
// read three numbers from the command line int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
if ((a > b) && (a > c)) { largest = a;
} else if (b > c) { largest = b;
} else {
largest = c;
}
System.out.println("Largest Number is:" + largest);
} }