



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Main points of this past exam are: Variables Number, Variable Names, Invalid Name, Balance Multiplied, Variable Interest, Java Statement, Output Produced, Program Code, Double Result, Boolean Expression
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Choose any two questions. All questions carry equal marks.
Examiners: Mr. T. Leane Dr. H. Gibbons Dr. M. O Moore
Q1. a) State whether each of the following is true or false. If false, explain why.
i) Comments cause the computer to print the text after the // on the screen when the program is executed. ii) All variables to be used in a program must be declared before they are used. iii) All variables must be given a type when they are declared. iv) Java considers the variables number and NuMbEr to be identical. v) The operators *, /, %, + and – all have the same level of precedence. (4 Marks)
b) Say whether each of the following variable names is valid or invalid and in the case of an invalid name give the reason why it is invalid:
i) 1stnumber ii) num1*num iii) guess?num iv) Avg (4 Marks)
c) i) Give the declaration for a variable called count of type int. The variable should be
initialised to zero in the declaration.
ii) Give the declaration for two variables of type double. The variables are to be named rate and time. Both variables should be initialised to zero in the declaration.
iii) Give a Java assignment statement that will set the value of the variable interest to the value of the variable balance multiplied by 0.05. The variables are of type double.
iv) Give a Java assignment statement that will set the value of the variable interest to the value of the variable (^) balance multiplied by the value of the variable (^) rate. The variables are of type double.
v) Give a Java statement that will increase the value of the variable count by 3. The variable is of type int. (7 Marks)
d) i) What is the output produced by the following lines of program code?
int quotient, remainder; quotient = 7/3; remainder = 7%3; System.out.println (“quotient = “ + quotient); System.out.println (“remainder = “ + remainder);
ii) What is the output produced by the following lines of program code?
double result; result = (6 / 9) * 2; System.out.println (“(6 / 9) * 2 equals “ + result); (7 Marks)
e) Given that number is a variable of type int:
int x, y, z; x = 2; y = 3; if (x > 2) if (y > 2) { z = x + y; System.out.println (“z is “ + z); } else System.out.println (“x is “ + x);
ii) Show the output of the following code, if any.
int x, y, z; x = 3; y = 2; if (x > 2) { if (y > 2) { z = x + y; System.out.println (“z is “ + z); } } else System.out.println (“x is “ + x); (7 Marks)
h) i) What output will be produced by the following code?
int count = 0; while (count < 5) { System.out.println (count); count = count + 1; } System.out.println (“count after loop = “ + count);
ii) What output will be produced by the following code?
int count = 0; while (count < 5) { System.out.println (count); count = count - 1; } System.out.println (“count after loop = “ + count); (7 Marks)
Q2. Write a Java program for each of the following:
a) Write a program which prompts the user to enter two integer values. The program then compares the two values and writes a message to the screen reporting which value is smaller. (10 Marks) b) Write a program which prompts for and reads 100 integer values. The program will determine the smallest value entered and the largest value entered. These two values will then be written in an appropriate format to the screen. The program should use an array. (20 Marks) c) Write a program which implements a simple guessing game. The program should invite the user to guess a target value between 1 and 100. This target value is hard coded in to the program. The program should continue to give the user guesses until the target value is guessed correctly. The program should give the user feedback on each guess by telling the user if the guess was too high, too low, or correct. The program ends when the user guesses correctly. (20 Marks)
Q3. a) Show how a computer would sort the following list of numbers in ascending order using
c) Given the following sorted array:
illustrate the operation of the binary search algorithm by showing in detail how it would search for each of the following values:
i) 27 (which is present in the array) ii) 107 (which is not present in the array) (15 marks)
Q4. a) Name and explain the steps you should go through when given a problem statement and asked
to write a computer program to solve that problem. (5 Marks)
b) Write a Java program for recording students’ marks. The class size is 40 students. The program will read the name of each student and an integer mark (0-100) for each student. The program will calculate the following:
(45 Marks)