0% found this document useful (0 votes)
61 views

CS140 40 10 F Sol

The document provides instructions for a final exam in Computer Programming 1. It includes information such as the course name and code, instructors, date, duration, marks allocation, and exam instructions. Students are to answer 4 questions in 7 pages within the 2 hour duration. The exam is closed book and notes. It will be marked out of 40 total marks.

Uploaded by

kiledcomw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

CS140 40 10 F Sol

The document provides instructions for a final exam in Computer Programming 1. It includes information such as the course name and code, instructors, date, duration, marks allocation, and exam instructions. Students are to answer 4 questions in 7 pages within the 2 hour duration. The exam is closed book and notes. It will be marked out of 40 total marks.

Uploaded by

kiledcomw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Al Imam Mohammad Ibn Saud Islamic University

College of Computer and Information Sciences


Computer Science Department
Course Title: Computer Programming 1
Course Code: CS140
Course Dr Alabbad, Dr Alanazi, Dr Alaql, Dr Alsayed, Dr
Instructors: Daadaa, Dr Shahin, Dr Sriti, Ms Alaboud, Ms
Albusayli, Ms Alaloula, Ms Alamro, Ms Almajed,
Ms Almutair, Ms Alqefari
Exam: Final Exam
Semester: Fall 2018
Date: 02/04/1440 - 09/12/2018
Duration: 2 hours
Marks: 40
Privileges: ☐ Open Book ☐ Open Notes
☐ Calculator Permitted ☐ Laptop Permitted

Student Name: MODEL ANSWER

Student ID:
Section No.:

Instructions:
1. Answer 4 questions; there are 4 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 10
2 10
3 8
4 12
Total 40

Page 1 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 1: To be answered in (25) Minutes [ ] / 10 Marks

Consider the following sequence number generated by 9 repeated calls of sr.nextInt(3) where
sr is a variable of type SecureRandom:

1 0 1 0 0 2 0 2 2

What is the output of the following code?

import java.security.SecureRandom;
public class Maze
{
public static void main(String[] args)
{
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
System.out.print(maze());
}
System.out.println();
}
System.out.println('^');
} // method main

public static char maze() {


SecureRandom sr = new SecureRandom();
int i = sr.nextInt(3);
switch(i) {
case 1 : return '_';
case 2 : return '|';
default: return ' ';
}
} // method maze
} // class Maze

Output: (1 mark for each character)

_ _

| |
^

Page 2 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 2: To be answered in (25) Minutes [ ] /10 Marks

The following Java program asks the user to enter a number between 1 and 10. If he/she enters a wrong
number, the program will ask him/her again and again for another number. The program ends when the
user enters a correct value.

There are 5 errors in this program. Find and fix the errors by filling the erroneous instruction line number
and its correction in the table below.

1 import java.util.Scanner
2
3 public static ReadValue {
4 public static void main(String[] args) {
5 Scanner input = new Scanner(System.in);
6 int n;
7 do
8 System.out.print("Enter a number between 1 and 10 = ");
9 n = nextInt();
10 } while(n < 1 && n > 10 );
11 }
12 }

Answer: (for each correct line 0.5, and for each correction 1.5)

Line # Error Correction

1 import java.util.Scanner;

3 public class ReadValue {

7 do {

9 n = input.nextInt();

10 } while(n < 1 || n > 10 );

Page 3 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 3: To be answered in (25) Minutes [ ] / 8 Marks

Write a Java method called abbreviate that receives a non-empty alphabetic string and returns its
abbreviation; a string composed of capitalized first letter of each word from the original string.

Call examples:

System.out.println(abbreviate("meters"));
System.out.println(abbreviate("Saudi Arabia"));
System.out.println(abbreviate("Stock keeping unit"));

Will output:

M
SA
SKU

You are not asked to write a main method.

Answer:

public static String abbreviate(String s) // 1 mark: header


{
// 1 mark: extract the first letter of the first word
String abbrev = s.substring(0,1); //or: ""+ s.charAt(0);

// 1 mark: find the index of the 2 nd word


int index = s.indexOf(" ");

while ( index != -1) // 1 mark


{
// 1 mark: extract the first letter of the following word
// 1 mark: accumulate the extracted letter with the previous ones
abbrev += s.charAt(index + 1);

// 1 mark: find the index of the following word


index = s.indexOf(" ", index + 1);
}
// 1 mark: return the accumulated string after its capitalization
return abbrev.toUpperCase();
}

Page 4 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 4: To be answered in (45) Minutes [ ] / 12 Marks

The program purpose of this question is to help students in elementary school to test their knowledge
about multiplication tables. Complete the following program by defining 2 methods fillAnswers and
correctAnswers where their descriptions are respectively provided in the next 2 pages.

import java.util.Scanner;

public class MultiplicationTest


{
public static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number between 1 and 10 = ");
int n = input.nextInt();

System.out.println("\nEnter your answers:");


int[] answers = new int[10];
fillAnswers(answers, n);

System.out.println("\nCheck your answers:");


System.out.printf("\nYour score is %d/10%n", correctAnswers(answers, n));
}

// fillAnswers method definition


...

// correctAnswers method definition


...
}

Page 5 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


a) Write a Java method called fillAnswers that takes an empty array answers (length = 10)
and a number n, then asks a student to enter the result of operation related to the multiplication
of the number n by numbers from 1 to 10 (n x 1, n x 2, n x 3, ..., n x 10). This
method saves the entered values in answers array.

Running example (student answers are entered after the “?” mark):

Enter your answers:


3 x 1 ? 3
3 x 2 ? 6
3 x 3 ? 9
3 x 4 ? 12
3 x 5 ? 16
3 x 6 ? 18
3 x 7 ? 21
3 x 8 ? 25
3 x 9 ? 27
3 x 10 ? 30

Answer (6 marks):

// student may define and use his/her own Scanner variable


public static void fillAnswers(int[] a, int n) // 1.5 mark
{
for(int i = 0; i < a.length; i++) // 1.5 mark
{
System.out.printf("%d x %d = ", n, i+1); // 1 mark

a[i] = input.nextInt(); // 2 marks: input (1) & assignment (1)


}
}

Page 6 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


a) Write a Java method called correctAnswers that takes an array answers (length = 10,
supposed already filled by fillAnswers method) and a number n (multiplied number related to
the results in answers), then prints students answers and checks whether if they were true
(correct answer) or false (wrong answer). The method must count and return the number of
correct answers.

Running example:

Check your answers:


3 x 1 = 3 => true
3 x 2 = 6 => true
3 x 3 = 9 => true
3 x 4 = 12 => true
3 x 5 = 16 => false
3 x 6 = 18 => true
3 x 7 = 21 => true
3 x 8 = 25 => false
3 x 9 = 27 => true
3 x 10 = 30 => true

Your score is 8/10

Answer (6 marks):

public static int correctAnswers(int[] a, int n) // 0.5 mark


{
int count = 0; // 0.5 mark: define/init the counter

for(int i = 0; i < a.length; i++) // 0.5 mark


{

boolean correct = false; // 0.5 mark: define/init bool var

if ( a[i] == n * (i+1) ) // 1 mark: check answers’ correctness


{
correct = true; // 0.5 mark: update bool var

++count; // 0.5 mark: increment the counter


}

// 1 mark: print result + evaluation (true of false)


System.out.printf("%d x %d = %d => %b%n", n, i+1, a[i], correct);
}
return count; // 1 mark
}

Page 7 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Page 8 of 8

Imam University | CCIS | Doc. No. 006-02-20170316

You might also like