preUPS 2 SET 2

You might also like

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

SET 2

PRE UPS II (SC025)

1 Identifier is the name that identifies the elements such as classes, methods, and
variables in a program that is written according to specific rules and naming
conventions.

Identify the invalid identifier and justify the reason based on the given declaration
statement.
[2 marks]
double gross_salary, income_tax, final, MonthlySalary;
_____________________________________________________________________
_____________________________________________________________________
2 Data types are especially important in programming because it helps prevent errors and
enhances reliability.

(a) Identify the data type for a variable named choice that can hold a single
character ‘Y’ or ‘N’ input by a user.
[1 mark]
_______________________________________________________________
(b) FIGURE 1 shows the output for a program that displays the value of variables
a and b. Write the declaration statement for variables a and b.
[1 mark]

FIGURE 1

_______________________________________________________________
(c) Write a declaration statement to declare a variable named no_athletes that
can hold a whole number.
[1 mark]
_______________________________________________________________

3 State two (2) types of operators that are used in the given Java expression.
[2 marks]
((value1 == 1) && (value2 == 2))
_____________________________________________________________________
_____________________________________________________________________
4 Write a Java statement to do the following:

(a) Increase the value of variable z by two.


[1 mark]
_______________________________________________________________
(b) A variable named age is between 13 and 19 (inclusive).
[2 marks]
_______________________________________________________________
5 Given the value of a variable a=3, state the output for the following expressions.
[2 marks]
(a) (((a * 2) – 3) > 2) && ((4 – 2) > 6)
____________________________________________________________
(b) ((30 / (a + 3 )) > 2) || ((20 % a) > 5)
____________________________________________________________
6 The program shown in FIGURE 1 has several errors.
1 class DivByZero {
2 public static void main(String args[])
3 {
4 int num1 = 15,num2 = 5,num3 = 0;
5 int ans1 = num1/num2;
6 int ans2 = num1/num3;
7
8 System.out.println "Division of num1"+ " by num2 is: "+ ans1;
9 System.out.println("Division of num1"+ " by num3 is: "+ ans2);
10 }
11 }

FIGURE 1

(a) Identify the line number that contains errors in FIGURE 1.


[1 mark]
_______________________________________________________________
_______________________________________________________________
(b) State and describe the reason why the errors occur in the above program.
[3 marks]
_______________________________________________________________
_______________________________________________________________
7 FIGURE 2 shows a diagram of an array.
num[0] num[1] num[2] num[3] num[4]

100 99 98 97 96 Y

X
FIGURE 2
(a) Identify X and Y.
[2 marks]
_______________________________________________________________
_______________________________________________________________
(b) State the arrayRefVar/array name.
[1 mark]
_______________________________________________________________

8 Write a Java statement (s) to declare and create a new array named marks to store
15 students’ marks that have decimal points.
[2 marks]
_____________________________________________________________________
_____________________________________________________________________

9 Answer the following questions based on the given Java statement.


double[ ]marks ={88.5,90.0,100.0,50.5,70.5};

(a) Write a Java statement to display the sum of the first two elements in the
given array.
[1 mark]
______________________________________________________________
(b) Assign the value 60.0 to the last element in the array.
[1 mark]
_______________________________________________________________
10 Write a loop statement that allows a user to enter data into an array named
NoOfStudent that holds 30 integer values. Assume all the variables and input (a
scanner object) have been properly declared.
[4 marks]
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

END OF QUESTIONS

You might also like