Get premium membership and access revision papers, questions with answers as well as video lessons.

Java Programming Question Paper

Java Programming 

Course:Bachelor Of Science In Informatics

Institution: Strathmore University question papers

Exam Year:2011




FACULTY OF INFORMATION TECHNOLOGY
Bachelor of Science in Informatics
END OF SEMESTER EXAMINATION
BIF 1202: JAVA PROGRAMMING
DATE: 22nd March, 2011 TIME: 2HRS

QUESTION 1
i. Show the syntax for importing one class and all classes in a package (2 marks)
ii. Describe the steps you take to run a Java application and the tools you use in each step.
What are source files and bytecode files? What different types of errors are
detected at each step? (4 marks)
iii. Using simple Java syntax, show the difference between object declaration and object
creation. (4marks)
iv. For each of these expressions, determine its result. Assume the value of text is a string
“Java Programming” (10 marks)
String text = “Java Programming”;
a. text.substring(0,4)
b. text.length()
c. text.substring(8,12)
d. text.substring(0,1) + text.substring(7,9)
e. text.substring(5,6) + text.substring(text.length()-3, text.length())
2

QUESTION 2
i. Determine the output of the following code (4 marks)
int x,y;
x=1;
y=2;
System.out.println(“The output is ” + x + y );
System.out.println(“The output is ” + ( x + y ));
ii. What will be the output from the following code? (5 marks)
class Q2Main {
public static void main(String args[]) {
QuestionTwo q2;
q2=new QuestionTwo();
q2.init();
q2.increment();
q2.increment();
System.out.println(q2.getCount());
}
}
Class QuestionTwo {
private int count;
public void init(){
count=1;
}
public void increment(){
3
count+=1;
}
public int getCount(){
return count;
}
}
iii. Which of the following three if statements are equivalent? (3 marks)
a. if (a ==b)
if (c == d) a = 1;
else b = 1;
b. if (a == b){
if (c == d) a = 1; }
else b =1;
c. if (a ==b)
if (c == d) a = 1;
else b = 1;
iv. Using nested if statement, write a Java program code to print the message “You did
pass” or “You did not pass” depending on the test score. If the test score is 70 or
higher, then we check the student’s age. If the age is 10, we will print “You did a
great job”. Otherwise, we will print “You did pass”, as before. (8 marks)

QUESTION 3
i. Write Java code for a program to compute the sum of the first 100 positive integers 1, 2… 100
(4 marks)
ii. What is wrong with the following program code? (4 marks)
4
int product = 0;
while (product < 500000){
product = product * 5;
}
iii. Show, using Java code, what you understand by the off-by-1 error (3 marks)
iv. Rewrite the following while loop as do-while loops (3 marks)
int count = 0, sum = 0;
while (count < 10){
sum += count;
count++;
}
v. Write a for loop to compute the following: (6 marks)
a. sum of 1,2, … , 100
b. sum of 2, 4, … , 500

QUESTION 4
i. What is an exception as used in Java programming? (2 marks)
ii. What are some 2 methods in the Throwable class that we can call to get some information
about the thrown exception? (2 marks)
iii. Illustrate using a simple Java code example how you would use the try-catch block control
statement (4 marks)
iv. What is the difference between checked and unchecked exceptions? (4 marks)
v. Name 3 types of assertions (3 marks)
vi. What is the output of the following demonstration? (5 marks)
class MultiDimArrayDemo {
5
public static void main(String[] args) {
String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}};
System.out.println(names[0][0] + names[1][0]);
System.out.println(names[0][2] + names[1][1]);
}
}






More Question Papers


Popular Exams


Mid Term Exams

End Term 1 Exams

End Term 3 Exams

Opener Exams

Full Set Exams



Return to Question Papers