Comp Programming Ii
Comp Programming Ii
SEMESTER: ONE
DURATION: 3 HOURS
INSTRUCTIONS:
(i) This paper contains two Sections: A (40 marks) & B (60 marks).
(ii) Attempt ALL questions in Section A, and ONLY THREE questions in
Section B.
(iii) All questions in Section B carry equal marks.
COMPUTER PROGRAMMING II
(iv) Credit will be given for use of relevant examples and illustrations.
(v) Begin each number in Section B on a new page of the answer sheet.
(vi) DO NOT write on this question paper.
COMPUTER PROGRAMMING II
Question 1
3. a) Give the syntax for the while loop and explain its semantics. (2 marks)
b) Given the program below:
public class MyClass{
int i = 30;
int j = 12;
public static void main(String[]args){
MyClass myObj1 = new MyClass();
MyClass myObj2 = new MyClass();
System.out.println(myObj1.i);
System.out.println(myObj2.j);
}
}
State the purpose of the following codes as used in the above program:
i) MyClass myObj1 = new MyClass(); (2 marks)
ii) System.out.println(myObj2.j); (2 marks)
iii) public class MyClass (2 marks)
Question 2
Study the following piece of java code and answer questions that follow:
public class SquareMain {
public static void main(String[] args) {
int result, n;
n = 3;
answer = square(n);
System.out.println("Square of 3 is: " + answer);
}
static int square(int i) {
return i * i;
}
}
Question 3
a) Explain the meaning of the following access specifiers:
i) public (2 marks)
ii) private (2 marks)
iii) protected (2 marks)
b) Distinguish the concept of single inheritance from multiple inheritance as used in
Java programming. (4 marks)
c) The quadratic equation , can be solved using the formula:
x = (-b + √(b2 – 4ac))/2a
COMPUTER PROGRAMMING II
Write a java program which can prompt the user to input values of a, b and c, and
to calculate and display the values of only where there exists real roots to the
equation. (10 marks)
Question 4
Study the flow chart below and use it to answer questions about it.
START
SUM = 0
N=0
N=N+1
SUM = SUM + N
YES
NO
IS N=10?
PRINT SUM
END
(a) Write a Java program used to implement the flow chart above (10 marks)
(b) Write a Java program used to grade soap detergents depending on weight in
grams as follows:
Question 5
COMPUTER PROGRAMMING II
a) Kalere sat three courses last semester and the average mark was used by his
sponsor to determine whether to fund his last semester only if the average is
greater or equal to 70 marks. Each course was marked out of 100 marks. Using
a while loop to compute total marks Kalere scored, write a program the sponsor
would use to determine Kalere’s sponsorship in the last semester.
(10 marks)
b) Carefully study the codes below and answer questions that follow:
public class WelcomeUICT
{
public static void main(String[ ] args)
{
System.out.println("Welcome to UICT Java Laboratory!");
System.exit(0);
}
}
(i) State the name of the class in the above codes (2 marks)
(ii) Write down the class header in the codes above. (2 marks)
(iii) Which class is used to give output of the above code. (2 marks)
(iv) Give the name of the type of methods used in the above code.
(2
marks)
(v) Explain the use of the key word “Static” in the program. (2 marks)
Question 6
a) Explain the difference between the for loop and do while loop in terms of
syntax and semantics. (4 marks)
b) Distinguish between a local variable and a global variable (2 marks)
c) State any four rules used in naming valid Java identifiers (4 marks)
d) Given the program below, re-write it using If – Else control structure.
(10
marks)
import java.util.Scanner;
public class CalculatorUsingSwitch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of 1st number ::");
COMPUTER PROGRAMMING II
int a = sc.nextInt();
System.out.println("Enter value of 2nd number ::");
int b = sc.nextInt();
System.out.println("Select operation");
System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");
char ch = sc.next().charAt(0);
switch(ch) {
if (ch== 'a')
{
System.out.println("Sum of the given two numbers: "+(a+b));
}
else if (ch== 's')
{
System.out.println("Difference between the two numbers: "+(a-b));
}
else if(ch== 'm')
{
System.out.println("Product of the two numbers: "+(a*b));
}
else if (ch=='d')
{
System.out.println("Result of the division: "+(a/b));
}
else
{
System.out.println("PLEASE ENTER CORRECT VALUE!");
}
}
}
END