Documente Academic
Documente Profesional
Documente Cultură
Major Exam 02
Saturday, August 7, 2010
Time: 100 minutes
Name: KEY
ID#:
Section 01 02
Maximum Obtained
Question #
Marks Marks
1 16 16
2 20 20
3 20 20
4 12 12
5 12 12
Total 80 80
~Good Luck~
2
a) How many times each of the following loops executes its body?
int x=1;
while(x<100){ 10
System.out.println(x+ “ ”);
x +=10;
}
int y = 250;
while(y % 3 !=0) Infinity
System.out.println(y);
String w=”a”;
while(w.length()<10) 5
w=”b”+w+”b”;
b) Show and explain the type of errors in the following code fragment.
int i;
int [] x = {1, 2, 3, 4};
for(i=1; i<=4; ){
System.out.println(x[i++]);
i++;
}
No syntax errors
No run time errors
So only logical errors which depend on what you need to do
c) What does the array numbers contain after the following code is executed?
int[] numbers = new int[8];
numbers[1]=4;
numbers[4]=99;
numbers[7]=2;
int x =numbers[1];
numbers[x] = 44;
numbers[numbers[7]]=11;
0 4 11 0 44 0 0 2
Err: System.out.println(first);
You need to print element by element using a loop
Err: if(first == second)
This compare the array references but not contents. To compare the
contents, you need to compare them element by element using a loop
4
Q2. [5 * 4 = 20 marks] Give output for each of the following program segments in the space provided:
Code Output
int x = 5, y=8; r=390625c=9
int c=0; r=1;
while(c++ != y)
r*=x;
System.out.println(“r=”+r+”c=”+c);
String st = “Sallam”;
for(int i = st.length() -1; i>0 ; i--)
System.out.print(st.charAt(i)+“ ”); m a l l a
}
System.out.print(”\n“); 4 8 12 16
int i, sum = 0;
for(i=0; i<10; i++){
switch(i){
case 0:
case 1:
case 3:
case 5:
sum++;
case 4:
break;
4
default:
break;
}
}
System.out.print(sum);
Q3. [20 marks] Design and write a Java program that prompts the user to enter some integer values.
Assume all values are positive except the last one. The program should store all the values except the
last one in a one-dimensional array. Then the program should print out all odd values followed by all
even values. Also if the maximum value is less than 20, the values are shown graphically as a bar
chart (each line represents a value by printing “*” a number of times equal to that value). To show
the values For example, if the user enters 7 8 9 3 10 -1, the program output should be as follows:
import java.util.Scanner;
public class Q3 {
//even values
System.out.print("Even values are:");
int even = 0;
for(int i=0; i<count; i++)
if(numbers[i]%2 == 0){
even ++;
if(even > 1)
System.out.print(", " + numbers[i]);
else
System.out.print(numbers[i]);
}
System.out.println();
//find maximum
int max = numbers[0];
for(int i=0; i<count; i++)
if(numbers[i] > max )
max = numbers [i];
//show graph
if (max < 20)
for(int i=0; i< count; i++){
for(int j = numbers[i]; j>0; j--)
System.out.print("*");
System.out.println();
}
}
}
}
Q4. [12 marks] Write a java program that prompts the user to enter the elements of a 2D array of
certain order NxM and to enter two values x and y. Then the program scan the array and prints out
the elements that have values in the range between x and y. The output should indicate the row and
column indices of each element followed by its value. For example, if the two users enter the
following 2D array and values for x and y:
1 9 3 2
5 3 7 4
2 4 8 3
x = 2, y = 5
0,2: 3
1,1: 3
1,3: 4
2,1: 4
2,3: 3
import java.util.Scanner;
public class Q4 {
}
}
9
Q5. [12 marks] Write a java program that prompts the user to enter a value for x and to enter an
integer value N representing the number of terms of the following sum:
Note that the sign is alternating. The program should calculate and print the above sum for the given
values. Design your program to avoid using the factorial function.
import java.util.Scanner;
public class Q5 {
double sum = 0;
double term = x;
int count = 1;
while(n>0){
sum += term;
n--;
count +=2;
term *= -x*x/(count*(count-1));
}
}
}