Solution

KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
‫كلية الحاسبات وتقنية المعلوما‬
Exam 1
Programming I (CPCS 202)
Instructor: M. G. Abbas Malik
Student Name: __________________________________
Date: October 18, 2015
Student ID: _____________________________________
Total Marks:
45
Obtained Marks: _________________________________
Instructions:

Do not open this exam booklet until you are directed to do so.

This exam will end in One and Half Hours (90 Mins).

Write your full name and Student registration No. clearly on the first page.

When the exam is started, write your complete Student Registration No. clearly on the top of *EVERY* page.

Write your solution in the space provided. If you need more space, write on the back of the sheet containing the problem. If still
you need more space then you can use extra sheets. In the case of extra sheet, clearly mention the question number whose answer
you are giving and you student registration number on the extra sheet.

Plan your time wisely. Do not spend too much time on any one problem. Read through all of them first and attack them in order
that allows you to make the most progress.

Show your work, as partial credit will be given. You will be graded not only on the correctness of your answer, but also on the
clarity and method correctness with which you express your answer.
Good luck.
1
KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
‫كلية الحاسبات وتقنية المعلومات‬
Question 1: Fill in the blanks
(30)
a) Consider the following code and give the out of the code:
(5)
Note that ASCII code of ‘a’ is 97.
publicclassExamPrint publicstaticvoidmain String
System.out.println "a"
System.out.println 'a'
System.out.println "a"
System.out.println "a"
System.out.println 'a'
args
1 ;
1 ;
1 1 ;
1 1 ;
1 1 ;
a1
98
a11
a2
99
b) How we can convert a String into Integer?
(1)
Integer.parseInt(stringInt) // a string is input to this method
c) How we can convert a String into Double?
(1)
Double.parseDouble(stringDouble) // a string is input to this method
d) A graphical representation of a computer program or an algorithm is called flow chart
(1)
e) Built-in Data Types of Java are called primitive data types.
(1)
f) What is the difference between Syntax Error and Runtime Error? Give one example for each.
(5)
Syntax Error: it is an error that is detected by the compiler. We have to correct it before we can compile our
program. Example:
int i = 23
Here semi-colon is missing and it is a syntax error.
Runtime Error: It is an error that cause the program to abort or stop at run time with error. Example:
Division by ZERO in the calculations is a run time error.
2
KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
‫كلية الحاسبات وتقنية المعلومات‬
g) Assume that variable int a = 5; and double d = 4.0; and int b = 2;
What are the values of the variables d and a after the execution of the following line?
(3)
d = 4 + d * d * b++ + 4 * ++a – 2
(1) d = 58
(2) a = 6
(3) b = 3
h) Consider the following code with type conversions and give the values of variables:
(3)
publicclassTypeConversions publicstaticvoidmain String args
inti 97;
doubled 124.76;
charc ‘A’;
c char i;
d 50;
i d;
(1) i = 174
(2) d = 174.76
(3) c = a
3
KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
i)
‫كلية الحاسبات وتقنية المعلومات‬
Finding the character of an ASCII Code: Write a program that receives an ASCII code (an integer between
0 and 128) and display its character. For example, if the user enters 97, the program displays character ‘a’ and
if the user enters 65, the program displays character ‘A’. Here is the sample run:
(5)
Enter an ASCII Integer code: 69
The character for ASCII code 69 is E
importjava.util.Scanner;
publicclassASCIICode publicstaticvoidmain String args intcode;
Scannerinput newScanner System.int ;
System.out.print “EnteranASCIIIntegercodebetween1and127” ;
code input.nextInt ;
charasciiCharacter char code;
System.out.print “ThecharacterforASCIIcode” code “is” asciiCharacter ;
`
4
KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
j)
‫كلية الحاسبات وتقنية المعلومات‬
Area of Hexagon: Write a program that prompts the user to enter the side of a hexagon and display its area.
The formula for computing the area of a Hexagon is
√
(5)
Note that you can use the Math.pow(a, 0.5) to compute √ . Here is the sample run:
Enter the side of Hexagon: 5.5
The area of Hexagon is 78.5895
importjava.util.Scanner;
publicclassAreaOfHexagon publicstaticvoidmain String args doubleside,area;
Scannerinput newScanner System.in ;
System.out.print “EnterthesideofHexagon:” ;
side input.nextDouble ;
area 3*Math.pow 3,0.5 /2 * side*side ;
System.out.print “TheareaofHexagonis” area ;
5
KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
‫كلية الحاسبات وتقنية المعلومات‬
Question 2 (Challenge question with Bonus Marks)
(15)
Area of a Triangle: Write a program that prompts the user to enter three points
,
,
,
and
,
of a triangle and display its area.
The formula to compute the distance between two points
,
and
,
is
.
The formula for computing the area of a triangle is:
1
2
,
3
2
1
1
2
3
3
Note that you can use the Math.pow(a, 0.5) to compute √ .
Here is the sample run of your program:
,
2
,
Please enter x coordinate of the first point: 1.5
Please enter y coordinate of the first point one: –3.4
Please enter x coordinate of the second point: 4.6
Please enter y coordinate of the second point: 5
Please enter x coordinate of the third point: 9.5
Please enter y coordinate of the third point: –3.4
The area of the triangle is 33.6
importjava.util.Scanner;
publicclassAreaOfTriangle publicstaticvoidmain String args doublex1,y1,x2,y2,x3,y3,side1,side2,side3,s,area;
Scannerinput newScanner System.in ;
System.out.print “Pleaseenterxcoordinateofthefirstpoint:” ;
x1 input.nextDouble ;
System.out.print “Pleaseenterycoordinateofthefirstpoint:” ;
y1 input.nextDouble ;
System.out.print “Pleaseenterxcoordinateofthesecondpoint:” ;
x2 input.nextDouble ;
System.out.print “Pleaseenterycoordinateofthesecondpoint:” ;
6
KINGDOM OF SAUDI ARABIA
Ministry of Higher Education
UNIVERSITY OF JEDDAH
Faculty of Computing and Information Technology
‫كلية الحاسبات وتقنية المعلومات‬
y2 input.nextDouble ;
System.out.print “Pleaseenterxcoordinateofthethirdpoint:” ;
x3 input.nextDouble ;
System.out.print “Pleaseenterycoordinateofthetpoint:” ;
y3 input.nextDouble ;
side1 Math.pow x2–x1 * x2–x1
y2–y1 & y2–y1 ,0.5 ;
side2 Math.pow x3–x2 * x3–x2
y3–y2 & y3–y2 ,0.5 ;
side3 Math.pow x3–x1 * x3–x1
y3–y1 & y3–y1 ,0.5 ;
s side1 side2 side3 /2;
area Math.pow s* s–side1 * s–side2 * s–side3 ,0.5 ;
System.out.print “Theareaofthetriangleis” area ;
7