بسم الله الرحمن الرحيم
كلية البيان – قسم نظم المعلومات
structured Programming With Java – sub exam
Question (1): (40 mark- 4 marks for each)
1- the following are java keywords- list other four java keywords?
public – void – main – for
- Static – while – if - class
2- list three of white-space characters?
- Blank lines , spaces, and tabs.
3- what is the output from the following statements ?
[Link]("This is “ );
[Link](“an Easy Test" );
This is an Easy Test
4- what is the value of d: d=(7+3*4 – 2*(3*4)/2))?
- d= 6
5- What we mean by comments in java? What is the benefits from comments? What are types of
Comments?
- Comments are lines that ignored by compilers.
- Improved program readability.
- The one line comment // and multiple line comments /*…………..*/
6- List the components of method header?
- Access modifier
- return type
- name of method
- parentheses
7- what is the difference between method’s parameters and method’s arguments?
- Methods parameters appear in method header. Whereas arguments appear in method call as
Values of parameters.
8- one kind of data types in java is primitive- this one contains many types ex: byte. List four others?
- Boolean , char, short, int , long, float, double
9- given the following statement: for (int n=12; n >=2;n -=2); Answer these questions:
- Name of the control variable is : n
- The initial value of the control variable is : 12
- Loop-continuation condition is: n >=2
10- what is the value of these Math methods: ceil(5.3); - floor(3.9) ?
- Ceil(5.3) = 6. Floor(3.9)= 3
Question (2): (20 mark- 2 marks for each)
1- With x = 0, which of the following are legal lines of Java code for changing the value of x to
1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
a) 1, 2 & 3
b) 1 & 4
c) 1, 2, 3 & 4
d) 3 & 2
Answer: d
2. . Decrement operator, –, decreases value of variable by what number?
a) 1
b) 2
c) 3
d) 4
Answer: a
3. Which of the following loops will execute the body of loop even when condition controlling
the loop is initially false?
a) do-while
b) while
c) for
d) None of the mentioned
Answer: a
4. . What is the output of this program?
1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. [Link](++g * 8);
6. }
7. }
a) 25
b) 24
c) 32
d) 33
Answer : c
5. Which of these statement is correct?
a) switch statement is more efficient than a set of nested ifs.
b) two case constants in the same switch can have identical values.
c) switch statement can only test for equality, whereas if statement can evaluate any type of
boolean expression.
d) it is possible to create a nested switch statements.
Answer: b
6-. Which of these have lowest precedence?
a) ()
b) ++
c) *
d) +
Answer: d
7. What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
Answer:c
8. What is the value stored in x in following lines of code?
int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
a) 0
b) 8
c) 9
d) 1
Answer: b
9- What is the output of this program?
1. class operators {
2. public static void main(String args[])
3. {
4. int var1 = 5;
5. int var2 = 6;
6. int var3;
7. var3 = ++ var2 * var1 / var2 + var2;
8. [Link](var3);
9. }
10. }
a) 10
b) 11
c) 12
d) 56
Answer:c
10- What is the output of this program?
1. class Output {
2. public static void main(String args[])
3. {
4. int a = 1;
5. int b = 2;
6. int c;
7. int d;
8. c = ++b;
9. d = a++;
10. c++;
11. b++;
12. ++a;
13. [Link](a + " " + b + " " + c);
14. }
15. }
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
Answer: d
Question (3): (20 mark- 2 marks for each)
Choose from B list the most suitable element that match A’s element.
B list : ++ n , Ignored by the compiler , n ++ , Java keyword , parameter , Invalid java
identifier, for , char , arguments, Valid java identifier
A list
1- comments Ignored by the compiler
2- while Java keyword
3- GradeBook Valid java identifier
4- method’s header parameter
5- preincrement ++ n
6- primitive data type char
7- white space character tab
8- method call arguments
9- post increment n ++
10- 4_you Invalid java identifier
Question (4): (20 mark- 5 marks for each)
1- write aprogram in java that display the following message:” Java is an Easy Language”?
public class MyClass{
public static void main(String args[]){
[Link](“Java is an Easy Language”);
}
}
2- Given this java code:
public class MyLoop{
public static void main(String args[]){
int total=0;
int counter =4;
while(counter<21){
total = total+counter;
counter= counter+2;
}// while
[Link](the total of even numbers is”,total);
}//main
}//class
This code calculate the total of even numbers between 3&21, rewrite this
code using while loop?
public class MyLoop{
public static void main(String args[]){
int total =0;
for(int i=4;i<=20;i+=2){
total+=I;
}// for
[Link](“the total of even numbers is”,total);
}//main
}//class
3- write a program using java, the program prompt the user to input three integers, the result
is the maximum number of these three integers: follow these guides:
a) write class MaximumFinder contains method determineMaximum to receipt these
integers.
b) write class MaximumFinderTest to call method determinemaximum method.
Public class MaximumFinder
{
Public void determineMaximum()
{
Scanner input = new Scanner([Link]);
[Link](“enter three integers”);
Int number1=[Link]();
Int number2=[Link]();
Int number3=[Link]();
Int result = maximum(number1,number2,number3);
[Link](“maximum result is :”+maximum);
}// method determinemaximum
Public class MaximumFinderTest{
Public static void main(String args []){
MaximumFinder mymaximum= new MaximumFinder();
[Link]();
}// main
4- what is the output of this code:
public class Grade
{
public static void main( String args[] )
{
int grade1 = 65;
int grade2 = 50;
[Link]( grade1 >= 60 ? "Passed." : "Failed." );
[Link]( grade2 >= 60 ? "Passed." : "Failed." );
} // end main
} // end class Grade
Answer:
Passed
Failed