Sunteți pe pagina 1din 9

1

‫ﺑﺴﻢ اﷲ اﻟﺮﺣﻤﻦ اﻟﺮﺣﻴﻢ‬


King Fahd University of Petroleum and Minerals
College of Computer Science and Engineering
Information and Computer Science Department
Summer Semester (093)
ICS 102 - Introduction to Computing I

Major Exam 02
Saturday, August 7, 2010
Time: 100 minutes

Name: KEY

ID#:

Please circle your section number below:

Section 01 02

Instructor Salah EL-Sayed

Day and Time SUMT SUMT


9:20 - 10:10 1030 - 11:20

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

Q1. [4 * 4 marks] Solve the following short answer questions:

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”;

for(int i = 1; i%5 ==0; i+=2) Zero


System.out.println(i);

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

d) What is wrong with the following code?


3

int[] first = new int[2];


first[0]=3;
first[1]=7;
int[] second = {3, 7};
System.out.println(first); //print the elements in the first array
if(first == second)
System.out.println(“the two arrays have the same elements”);
else
System.out.println(“the two arrays have different elements”);

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

for(int i=0; i<4; i++){


int n = 1;
do{ ***********
System.out.print(“*”); (to infinity)
n++;
} while(n!=i);
}
int i, j;
for(i=1; i<5; i++){
for(j=1; j<=i; j++){ 1
if(i==3)
continue; 2 4
System.out.print(i*j + ” “);
}

}
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);

String Class Cheat Sheet Cont… Math Class Cheat Sheet


length() indexOf(String) PI , E
compareTo(String) indexOf(String, int) pow(double, double)
compareToIgnoreCase(String) lastIndexOf(String) abs(double)
equals(String) charAt(int) min(double, double)
equalsIgnoreCase(String) substring(int) max(double, double)
toLowerCase() substring(int, int) round(double)
toUpperCase() trim() sqrt(double)
5

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:

You entered 5 positive integers


Odd values are: 7, 9, 3
Even values are: 8, 10
*******
********
*********
***
**********

import java.util.Scanner;

public class Q3 {

public static void main(String[] args) {

Scanner kb= new Scanner(System.in);


int x; int count = 0;
int [] numbers = new int[200];

// read the numbers


System.out.println("Enter some positive integers then a negative to end:");
while(count<200 && (x=kb.nextInt()) > 0) {
numbers[count] = x;
count++;
}
System.out.println("You have entered "+count+ " positive integers");

if (count > 0){


//odd values
System.out.print("Odd values are:");
int odd = 0;
for(int i=0; i<count; i++)
if(numbers[i]%2 != 0){
odd ++;
if(odd > 1)
System.out.print(", " + numbers[i]);
else
System.out.print(numbers[i]);
}
System.out.println();
6

//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();
}
}
}
}

P.S. some operations can be merged as well


7

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

Then the output will be as follows:

0,2: 3
1,1: 3
1,3: 4
2,1: 4
2,3: 3

import java.util.Scanner;

public class Q4 {

public static void main(String[] args) {

int [][] numbers = new int[200][200];

Scanner kb= new Scanner(System.in);

// read the numbers


System.out.println("Enter the matrix order (m,n) less than 200x200:");
int m =kb.nextInt();
int n =kb.nextInt();

System.out.println("Enter the matrix elements (row by row):");


for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
numbers[i][j] = kb.nextInt();

System.out.println("Enter the range (x,y) such that x < y:");


int x = kb.nextInt();
int y = kb.nextInt();
8

System.out.println("Elements in the range are:");

for(int i = 0; i < m; i++)


for(int j = 0; j < n; j++)
if(numbers[i][j]>x && numbers[i][j] < y)
System.out.println(i + "," + j + ":" + numbers[i][j]);

}
}
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:

Sum = x – x3/3! + x5/5! – x7/7! + ….

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 {

public static void main(String[] args) {

Scanner kb= new Scanner(System.in);

// read the numbers


System.out.println("Enter the value for x and n:");
double x =kb.nextDouble();
int n =kb.nextInt();

double sum = 0;
double term = x;
int count = 1;

while(n>0){
sum += term;
n--;
count +=2;
term *= -x*x/(count*(count-1));
}

System.out.println("sum = " + sum);

}
}

S-ar putea să vă placă și