FinalExam Previous

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Al Imam Mohammad Ibn Saud Islamic University

College of Computer and Information Sciences


Computer Science Department

Course Title: Introduction into Programming / Computer


Programming 1
Course Code: CS 140 / CS 150
Course Instructor: Mr.Abdulelah Abuabaat, Mr.Alhadi Suliman,
Ms.Amal Alamro, Dr.Amal Alsaif, Dr.Asma
Alsaleh, Dr.Basma Alqadi, Ms.Eman Albader,
Dr.Eman Alzaid, Dr.Manal Alsabhan, Dr.Raad
Alturki, Dr.Saleh Alghamdi, Dr.Sultan
Alahmari, Dr.Waad Alhoshan, Dr.Yaseen
Daadaa
Exam: Final Exam
Semester: First Semester Fall 2021
Date: 20- December 2021
Duration: 120 Min. (8:00 am - 10:00 am)
Marks: 40
Privileges: ☐ Open Book ☐ Open Notes
**Calculator Permitted ☐ Laptop Permitted

Student Name :

Student ID:
Section No.:

Instructions:

1. Answer 1_ questions; there are 3 questions in _7_ pages.


2. Write your name on each page of the exam paper.
3. Write your answers directly on the question sheets. Use the ends of the question pages for
rough work or if you need extra space for your answer.
4. If information appears to be missing from a question, make a reasonable assumption, state
your assumption, and proceed.
5. No questions will be answered by the invigilator(s) during the exam period.

Official Use Only


Question Student Marks Question Marks
1 15
11 15
111 10
Total 40

Page 1 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Student Name (in English): __________________________________________ Student ID: _____________________________

Question 1: Trace the output To be answered in (40) Minutes [ ] / _15_ Marks

Choose one correct answer for each statement and fill the table below.

Question 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Answer

1- What is the output of the following java code?


int a = 2;
boolean b = true;
if ((b) || (--a> 0))
System.out.print(a);
else
System.out.print("False");
a) false
b) 2
c) 1
d) A

2- What is the output of the following java code?


String s1 = "Lets learn Java";
System.out.println((s1.substring(5,10)).startsWith("J"));
a) true
b) false
c) Java
d) Learn

3- The number of times the word “loop” will be printed in the following code is:
for (int i = 0; i< = 10; i--)
System.out.println(“loop”);
a) 0
b) 10
c) 11
d) Infinite

4- How many lines will be printed by the code below?


int j = 0;
for (; j <= 4 ; )System.out.println(j++);
a) 0
b) 4
c) 5
d) infinite

Page 2 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Student Name (in English): __________________________________________ Student ID: _____________________________

5- What is the output of the following java code?


int a = 15; int b = 20; int c = 22;
if(a>b)
if(a>c)
System.out.print("1111");
else
System.out.print("2222");
a) 1111
b) 2222
c) 11112222
d) Nothing

6- What is the output of the following java code?


int[] a = {1, 2, 3, 4};
System.out.print(Arrays.binarySearch(a, 0));
a) -1
b) 0
c) 1
d) [1, 2, 3, 4]

7- What is the output the following java code?


int[][] b = { { 1, 2 }, { 3, 4, 5 } };
System.out.print(b[1][1]);
a) 1
b) 2
c) 3
d) 4

8- The outpu of the following line is ..


System.out.printf(“%.4f%n”, 45.13);
a) 45.1300
b) 45.13
c) 45.00
d) 45

9- What is the output of the following java code?


int[] a = new int [5];
System.out.print(a[2]);
a) a[2]
b) 0
c) random number
d) cannot tell

10- What is the output of the following java code?


int a = 1; int b =2;
System.out.println(a > b ? "a":"b");
a) a
b) b
c) 1
d) 2

Page 3 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Student Name (in English): __________________________________________ Student ID: _____________________________

11- What is the output of the following java code?


int[] a = {1, 2, 3, 4};
Arrays.fill(a,1);
System.out.print(a[3]);
a) 0
b) 1
c) 4
d) a[3]

12- The output of the following code:


int[] b = {4,5,2,1};
for (int i=2; i>=0; i--)
System.out.print( b[i] + b[i+1] + " ");
a) 3 7 9
b) 9 7 3
c) 3 7 9 7
d) Compile error.
13- The output of the following code
char[] code = {'A','B','C'};
for(char L: code) System.out.print(++L);

a) CBA
b) ABC
c) BCD
d) DCB
14- The output of the following code
int x=2;
double y=9;
System.out.print(y/x);
a) 4
b) 4.5
c) 5
d) Compile Error

15 - Which method will be invoked by the code below?


int a = 1; boolean b = true; double c = 3.4;
methodOne(a, b, c) ;
a) static void methodOne(double x, boolean y, double z)
b) static void methodOne(double x, boolean y)
c) static void methodOne(double x, int y, double z)
d) static void methodTwo(double x, boolean y, double z)

Page 4 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Student Name (in English): __________________________________________ Student ID: _____________________________

Question 11: Codes Completion To be answered in (35) Minutes [ ] / _15_ Marks

1- Consider the following headers, how would you call these methods in your class’s main method. [2 marks]

public static void h1(boolean v, int x)

public static double h2(String a, int[] b)

2- Consider the two methods below and write their headers without their body codes. [2 marks]

A method called add3 takes three integer numbers


and returns the sum of three integer parameters.

A method called randomArray receives an array of


integers and a random value as integer and then sets
the array elements to have the same random value.

3- Consider the methods below and write their headers and body codes as described. [5 marks]

Write a method g that computes the result of


three floating-point numbers as g(x, y,z)
where g(x, y,z) = x 3 + √3𝑦+z. The method
should return the results to its caller.

Write SumUp method that receives an 2D-array


of double elements and returns the total of
adding up all the matrix elements. Considering
that the rows in the matrix might have different
lengths.

Page 5 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Student Name (in English): __________________________________________ Student ID: _____________________________

4- The following code is implemented using for-loop, convert this code to be a recursion method called
GetSum [ 3 marks]

for(int i=1; i<= n; i++){sum+=i;}

5- Convert the following formula method to accept any number of int arguments. [ 3 marks]

public static int formula(double h, int x, int y, int z){


return h*(x+y+z);
}
public static int formula(double h, int x, int y){
return h*(x+y);
}

Page 6 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Student Name (in English): __________________________________________ Student ID: _____________________________

Question 111: Write a java class To be answered in (35) Minutes [ ] / _10_ Marks

Write a java class called Products that reads product information and extracts products information
and print it to the user. The product code consists of the country initials, the product code followed by
the product serial number, product code example: UK-001-176

Your class should contain One Method plus the main method.

ExtractInfo that receives a product code as a String. The method should extract the origin country
of the product, its code and then the product serial number and prints out the result and then saves the
same result into a file called “Info.txt” as shown below

ExtractInfo(“UK-001-176”) prints and saves the result as

Country: UK, Code:001, Serial: 176

In the main method:


• Ask the user to enter a product code.
• Then, call ExtractInfo method to extract, print, and save the product information.

Page 7 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
The end of final exam, good luck!

Page 8 of 9
Imam University | CCIS | Doc. No. 006-01-20141026
Java String Methods
http://introcs.cs.princeton.edu/java/11cheatsheet/

int length() number of characters


char charAt(int i) the character at index i
String substring(int i, int j) characters at indices i through j-1
boolean startsWith(String pre) does this string start with pre?
boolean endsWith(String post) does this string end with post?
int indexOf(String pattern) index of first occurrence of pattern
int indexOf(String pattern, int index of first occurrence of pattern starting from
i) i
int lastIndexOf(String pattern) index of last occurrence of pattern
int lastIndexOf(String index of last occurrence of pattern ending at i
pattern,int i)
String concat(String t) this string with t appended
int compareTo(String t) string comparison
String toLowerCase() this string, with lowercase letters

String toUpperCase() this string, this uppercase letters


boolean equals(Object t) is this string’s value the same as t’s?
boolean equalsIgnoreCase(String t) is this string’s value the same as t’s? (ignoring
the case)
String replace(String a, String b) this string with as replaced by bs
boolean regionMatches(int toffset, true if len characters in other string starting
String other, int ooffset, from ooffset are the same in this string starting
int len) from toffset (case sensitive)

boolean regionMatches(boolean true if len characters in other string starting


ignoreCase, int toffset, from ooffset are the same in this string starting
String other, int ooffset, from toffset (ignore case)
int len)
String s.trim() Returns a copy of the string, with leading
and trailing whitespace omitted.
char[] s.toCharArray() To convert a string into a character array
void s.getChars(int sStart, int To copy specific characters from String
sEnd, char[] dest, int into dest as character array.
destStart)

Page 9 of 9
Imam University | CCIS | Doc. No. 006-01-20141026

You might also like