KEMBAR78
01A ComputerProgrammingI Exam Final 2024 | PDF | Computer Engineering | Mathematical Logic
0% found this document useful (0 votes)
13 views8 pages

01A ComputerProgrammingI Exam Final 2024

فهم اساسيات البرمجه لفهم قواعد الادوات الأمنية السيبراني

Uploaded by

alhasaqwp
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)
13 views8 pages

01A ComputerProgrammingI Exam Final 2024

فهم اساسيات البرمجه لفهم قواعد الادوات الأمنية السيبراني

Uploaded by

alhasaqwp
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

Department: CS, IS, and CYS (1) ‫ﻧﻣوذج رﻗم‬

Odd Semester Examination – Academic year 2023/2024


Student Name Reg. No
Subject Computer Programming I
Class B1CS, B1IS, and B1CYS Date 04/01/2024
Max. Marks 50 Time 2 hours
Lecturer Dr. Wadah Munassar Signature

Question No. 1 2 3 4 5 6 Total


Maximum Marks
Marks Scored

INSTRUCTION TO CANDIDATE:
˗ Answer question number six COMPULSORY and any other THREE
questions

Question 1:
Q1A) Circle the best answer.
1) What is the return type of the Math.random() function?
A.boolean B. int C.float D.double
2) What will be the output of the following code snippet?
int age = 25;
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result);
A) Adult B) Minor C) 25 D)No output will be generated.
3) Which of the following is an example of a valid variable name in Java?
A. 123variable B. _myVariable C. my-variable D. MyVariable&
4) Which of the Boolean expressions below is incorrect?
A. (true) && (3 >= 4)
B. !(x > 0) && (x > 0)
C. (x > 0) || (x < 0)
D. (x != 0) || (x = 0)
5) Which of the following declares a char named constant called TOP_GRADE?
A. final char TOP_GRADE = 'A';
B. final char TOP_GRADE = "A";
C. final char TOP_GRADE;
D. both A and C

Q1B) What is the output of the following while loop?

int sum = 0, i = 1;
while (i < 10) {
sum = sum + i;
}

1
Question 2:
Q2A) Match each word or concept in column A to the correct description or
definition in column B.

Column A Column B
1) It selects one of many code blocks to be executed based
A) source code on the value of an expression.
2) A tool that programmers use to help them plan an
algorithm.
3) An error that occurs while a program is running if the
environment detects an operation that is impossible to
B) flowchart carry out.
4) It is a program written in a high-level programming
language.
C) syntax error 5) Loop inside another loop.
6) It executes a block of code if a specified condition is true.
7) A tool that programmers use to help them depict an
D) for loop algorithm.
8) An error that occurs when a program instruction violates
a programming language’s syntax
9) A memory location whose value cannot be changed while
a program running.
E) switch statement 10) Executes a set of statements repeatedly until a condition
becomes false, with the condition checked at the
beginning

Q2B) Suppose that the input is:


38 35 71 14 -1
What is the output of the following code? (Assume all variables are properly
declared.)
var num = input.nextInt();
var sum = input.nextInt();

while (num != -1) {


sum = sum + num;
num = input.nextInt(); }
System.out.println("Sum = " + sum);

Question 3:
Q3A) State whether the following statement is true or false. If it is false, EXPLAIN
why?
1) This expression (x > 0 && x < 10) is the same as ((x > 0) && (x < 10))?
2) The result of a logical expression cannot be assigned to an int variable.
3) Every if statement must have a corresponding else.
4) In an infinite while loop, the loop condition is initially false, but after the first
iteration, it is always true.
5) Pseudocode is used to write program steps.

2
Q3B) Convert the following for loop into while loop

for (int i = 1; i <= 10; i++) {


for (int j = 1; j <= 10; j++) {
int product = i * j;
System.out.print(product + " ");
}
System.out.println();
}

. .

Question 4: Convert the following flowchart into algorithm or pseudocode:

Start

Get random num


from ( 1 – 10 )

Input guess

False while True


guess != random

Display
Display “Sorry, guess again:”
“Yes, the number is” +
random

Input guess

End

3
Answer for question 4:

Question 5: Design an algorithm (pseudocode), draw a flowchart and write code


for a program that prompts the user to input a number. The program
should then output the number and a message saying whether the
number is positive, negative, or zero.

Answer for question 5:

4
5
Question 6: Draw a flowchart and write a program that implements a Product
(Multiplication) Game. The program should calculate a product of
five numbers generated randomly between 1 and 10, and the user
will have 5 chances to input the correct answer. If the user answer
correctly within the given chances, the program should display a
congratulatory message. If the user runs out of chances without
input the correct answer, the program should display an appropriate
message along with the correct product of the five numbers.

Your task is to draw and write a code to implement the Product Game based
on the following pseudocode:
(Note: Assume that the necessary import statements have been included.)

Algorithm (Pseudocode):
Step01: Start
Step02: Generate five random numbers between 1 and 10 and assign them to num1,
num2, num3, num4, and num5.
Step03: Set the variable 'chances' to 5.
Step04: Calculate the correct answer as product = num1 * num2 * num3 * num4 *
num5.
Step05: while 'chances' is greater than zero:
Prompt the user to enter the answer by displaying "Enter your answer: "
Read the user's answer and store it in the variable 'UserAnswer.'
If 'UserAnswer' is equal to ‘product’
Display the message "Congratulations! You got the correct
answer".
Set 'chances' to -1 to exit the program.
Else:
Decrement 'chances' by 1.
If 'chances' is greater than zero:
Display the message "Incorrect answer! Try again. Chances
remaining: " + chances
Display the message "You have " followed by the value of
'chances' and " chance(s)
Step06: If 'chances' is equal to zero:
Display the message "Sorry, you ran out of chances. The correct answer
is: " + product
Step07: End

6
Answer for question 6:

7
Good Luck…

You might also like