2011 Past Paper

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

UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


Academic Year 2010 /2011 – 1st Year Examination – Semester 2

IT2204 - Programming I
30th July 2011
(TWO HOURS)

Important Instructions :
 The duration of the paper is 2 (two) hours.
 The medium of instruction and questions is English.
 The paper has 45 questions and 12 pages.
 All questions are of the MCQ (Multiple Choice Questions) type.
 All questions should be answered.
 Each question will have 5 (five) choices with one or more correct answers.
 All questions will carry equal marks.
 There will be a penalty for incorrect responses to discourage guessing.
 The mark given for a question will vary from 0 (All the incorrect choices are
marked & no correct choices are marked) to +1 (All the correct choices are
marked & no incorrect choices are marked).
 Answers should be marked on the special answer sheet provided.
 Note that questions appear on both sides of the paper.
If a page is not printed, please inform the supervisor immediately.
 Mark the correct choices on the question paper first and then transfer them
to the given answer sheet which will be machine marked. Please
completely read and follow the instructions given on the other side
of the answer sheet before you shade your correct choices.
1) Select from among the following, the correct option/s, that can be considered as (a) key
word/s in Java.

(a) main (b) final (c) false


(d) short (e) true

2) Consider the following path which is appearing in a Windows environment.

C:\Program Files\Java\jdk1.5.0_22\bin

Then consider the following working folder and its path of a student called Vimukthi
Jayaweera and assume that all his Java source files are saved in his working folder. Further
assume that Vimukthi has no administrative privileges in the computer.

C:\vimukthijayaweera\

Select from among the following, (a) suitable place/s, which can be used to write the set
path command in order to compile a Java file written by Vimukthi Jayaweera.

(a) C:\Program Files\Java\jdk1.5.0_22\bin (b) C:\Program Files\Java


(c) C:\Program Files (d) C:\Program Files\Java\jdk1.5.0_22
(e) C:\vimukthijayaweera\

3) One has typed the following command in the command prompt in a windows based
computer where Java software is installed.

java

Select from among the following, the output which can be seen in the command prompt as a list.

(a) –version (b) –client (c) –server


(d) –showversion (e) –classpath

4) Select from among the following, the option/s which can be considered as reference data
type/s in Java.

(a) int (b) String (c) byte


(d) boolean (e) float

5) Select from among the following, valid assignments allowed in Java. Assume that all the variables
are properly declared.

(a) int value = value + 55; (b) a =+ 55; (c) mark = mark + 45;
(d) result += 56; (e) value = 78;

2
6) Consider the following program written in Java.

public class FirstProgram{


public static void main(String args[]){

System.out.println("Vimukthi Jayaweera");

}
}

Select from among the following, the correct option/s on modifying the above program without
making compilation errors.

(a) public class FirstProgram  class FirstProgram


(b) public static void main(String args[])  private static void main(String args[])
(c) System.out.println(“Vimukthi Jayaweera”);  println.System (“Vimukthi Jayaweera”);
(d) System.out.println(“Vimukthi Jayaweera”);  System.out.print("Vimukthi Jayaweera");
(e) public static void main(String args[])  public static void main(String names[])

7) Select from among the following, the correct statement/s considering Windows and Linux
Operating systems and Java programming language.

(a) In Windows Operating system, one can use Notepad editor and in Linux one cannot
find Notepad editor to write the source code.
(b) When saving the source code in Windows, java file extension is required to type but in the
Linux environment it is not necessary to type the file extension.
(c) javac command is not a valid command in Linux but in Windows, it is a valid command in
compiling the Java source code.
(d) In a Linux Operating system, one cannot find eclipse or net beans like IDEs similar to
Windows Operating system.
(e) In a Linux operating system, the Java bytecode file will have the file extension .obj unlike
in Windows operating systems where one can see the file extension .oak.

8) Select from among the following, the operator/s which can be categorized as bit-wise operators in
Java programming language.

(a) | (b) || (c) &


(d) && (e) >>

9) Select from among the following, the name/s which have an impact on development of the Java
programming language.

(a) Oak (b) banana (c) James Gosling


(d) AT & T lab (e) Green project

3
Use the following declarations and initializations to evaluate the Java expressions given in
questions 10 - 14. Assume that each expression is evaluated separately in the program.
int a = 10,b = 15,c = 20;
byte m = 1;
float k = 10.0f;
char ch = 'A'; // note that the ASCII value of A is 65

10) System.out.println(a | b);

(a) false (b) 10 (c) 15


(d) 25 (e) error

11) System.out.println(k = k++);

(a) 10 (b) 11.0 (c) 10.0


(d) false (e) error

12) System.out.println(a * m * k);

(a) 100 (b) 100.0 (c) true


(d) 10.0 (e) error

13) System.out.println(c < ch);

(a) 85 (b) 65 (c) A


(d) true (e) error

14) System.out.println(c < ch > a);

(a) true (b) false (c) 65


(d) 10 (e) error

Consider the following pool of Java statements to answer questions 15 – 20. Note that each
statement is given a unique number as an identifier. In each question a problem is given and
in order to solve that problem one has to write segments of Java programs according to the
given instructions. It is not required to consider writing the class name or main method in the
program. Then in the answer each option is given a list of identifier numbers indicating the
program statements.

Identifier Java statements/Curly Brackets


1 }
2 {
3 for(int i=0;i<10;i++)
4 for(int i=0;i<=10;i++)
5 System.out.println(i+1);
6 for(int i=1;i<=10;i++)
7 for(int i=10 ; i >= 0 ;i--)
8 System.out.println(i);
9 for(int i=100 ; i >= 0 ;i--)
10 for(int i=100 ; i > 0 ;i--)
11 if( i % 2 == 0)

4
12 if( i / 2 == 0)
13 System.out.println(i);
14 int total=0;
15 for(int i = 50 ; i <= 60 ; i++)
16 for(int i = 50 ; i < 60 ; i++)
17 if( i % 2 = 0)
18 total = total + i;
19 System.out.println(total);
20 int number1 = 10 , number2 = 20 , number3 = 30;
21 if(number1 > number2 && number1 > number3)
22 System.out.println(number1);
23 else if(number2 > number3)
24 System.out.println(number2);
25 else
26 System.out.println(number3);
27 int num1 = 1, num2 = 5;
28 char op = '+';
29 switch(op)
30 case '+': System.out.println(num1 + num2); break;
31 case +: System.out.println(num1 + num2); break;
32 case '-': System.out.println(num1 - num2); break;
33 case -: System.out.println(num1 - num2); break;
34 case '/': if(num2 != 0)
System.out.println(num1 / num2);
break;
35 case /: if(num2 != 0)
System.out.println(num1 / num2);
break;
36 case '*': System.out.println(num1 * num2); break;
37 case *: System.out.println(num1 * num2); break;
38 default:System.out.println("Wrong Operator");
39 break:System.out.println("Wrong Operator");
40 total +=i;
41 for(int i = 50 ; i < 61 ; i++)
42 if(number2 > number1 && number2 > number3)
43 else if(number1 > number3)

15) Write a Java program to print the number series from 1 to 10 in the command prompt vertically.
The control variable name should be i and it should be initialized to 0. Using a for control structure
one has to write the program.

How should the blank space in the following program be filled?

class Ex15{
public static void main(String args[]){ blank } }

(a) 3,4 (b) 6,7


(c) 8,9 (d) 3,5
(e) 3,8

5
16) Write a Java program to show number series 10 to 1 in the command prompt vertically. (10 9 8 7 6
5 4 3 2 1). Name of the control variable should be i and initialized it to 10. Using a for control
structure one has to write the program.

How should the blank space in the following program be filled?

class Ex16{
public static void main(String args[]){ blank } }

(a) 4,5 (b) 6,7


(c) 7,8 (d) 3,8
(e) 3,4

17) Write a Java program to find all the even numbers (numbers which can be divided by 2 without any
remainders) from the number series 100 to 0 in the command prompt. Numbers should be printed in
the reverse order. E.g. 100 98…0. Using a for control structure one has to resolve the problem.
Name the control variable as i.

How should the blank space in the following program be filled?

class Ex17{
public static void main(String args[]){ blank } }

(a) 8,11,1,13,2 (b) 7,1,12,13,2


(c) 1,2,14,11,12 (d) 8,1,14,13,2
(e) 9,1,11,13,2

18) Write a Java program to get the total of even numbers in the number series 50 to 60 including 50
and 60. The program has 2 variables. One variable named i is suppose to control flow. The other
variable is total to retain the total of all even numbers in the specified range. The program should
output only 330 as the total. One has to solve the problem using a for control structure.

How should the blank space in the following program be filled?

class Ex18{
public static void main(String args[]){ blank } }

(a) 13, 14, 1, 16, 19, 2, 20 (b) 11, 12, 1, 15, 16, 2, 19
(c) 14, 41, 1, 11, 40, 2, 19 (d) 14, 15, 1, 11, 18, 2, 19
(e) 9, 11, 1, 15, 2, 14, 19

19) Write a Java program to find the largest number out of 3 given numbers. The three numbers are 10,
20 and 30. One has to declare 3 variables namely number1, number2 and number3 to store those
whole numbers. Using nested if control structure one has to evaluate the problem.

How should the blank space in the following program be filled?

class Ex19{
public static void main(String args[]){ blank } }

(a) 20, 11, 12, 23, 2, 25, 26 (b) 2, 28, 1, 33, 34, 35, 36
(c) 28, 1, 12, 23, 2, 21 (d) 20, 42, 24, 43, 22, 25, 26
(e) 20, 21, 22, 23, 24, 25, 26

6
20) Write a Java program to simulate a simple calculator. The calculator has 2 numbers declared,
namely num1 and num2 and initialized with whole numbers 1 and 5. The operator is stored in a
variable having the name op and assigned with an operator +. The program should simulate
addition, subtraction, division and multiplication tasks. When division functionality is coded,
division by zero error should be considered. Using the switch control statement one has to solve the
problem.

How should the blank space in the following program be filled?

class Ex20{
public static void main(String args[]){ blank } }

(a) 26, 27, 28, 29, 30, 31,32, 33, 34 (b) 25, 26, 27, 28, 29, 34,30 31, 32
(c) 27, 28, 29, 1, 30, 32, 34, 36, 38, 2 (d) 27, 28, 29, 1, 31, 33, 35, 37, 39, 2
(e) 1, 5, 23, 24, 25, 26, 27, 2, 12

21) Select from among the following, the notation/s which is/are common to both Nassi-shneidernan
diagrams and Flow Charts.

(a) (b)

(c) (d)

(e)

22) Consider the following statement.

Total = Total + Counter

Then read the following observation.

“The data items Total and Counter must already have been defined within the program”

Select from among the following the kind of data definition which is mentioned in the above
observation.

(a) Explicit (b) Abstraction (c) String


(d) Implicit (e) Array

23) Select from among the following, (a) correct statement/s which describe/s the kind of a collection
called Maps.

(a) A Map is an object that one can use once to retrieve all the objects in a collection one by
one.
(b) In an entry of a Map, a pair of objects is involved.
(c) In Java collection framework, a Map is implemented as a priority queue.
(d) The key determination process in a Map is called hashing.
(e) A Map, linked list, Vector and a queue can be considered as lists in collection framework.

7
24) Consider the following diagram noting the outer circles with roman numbers. The diagram
illustrates to use some basic set of criteria for selecting notations to be used in algorithm
specification.

(i)
Rules for

(iv) choosing
notations in
algorithm
specification
(ii)

(iii)
Select from among the following, the valid options to fill the circles numbered i – iv in roman.

(a) (i)  conciseness, (ii)  unambiguity, (iii)  capable in machine execution, (iv) 
promoting the elegance in the solution.
(b) (i)  Modular, (ii)  Art form, (iii)  Structured,(iv)  Object orientation
(c) (i)  Nassi-shneidernan, (ii)  Pseudocode, (iii)  Flow Chart, (iv)  NS diagram
(d) (i)  Process, (ii)  Decision, (iii)  Termination, (iv)  Flow of Logic
(e) (i)  Input employee details, (ii)  Compute gross pay, (iii)  Compute tax, (iv) 
Compute net pay.

25) Select from among the following, the valid method/s which is/are bundled with Stack storage
collection.

(a) T push(T obj) (b) T pop() (c) T peek()


(d) int search(Object obj) (e) boolean empty()

8
Consider the following class declarations to answer questions 26 – 35.

abstract class J{
private int varA1;
private int varA2;
J(){}
public final void methodA1(int x){
varA1=x;
}
public abstract void methodA2(int y){
varA2=y;
}
}

final class K extends J{


private String varB1;

K(){}
public abstract void methodB1(String z){
varB1=z;
}
public final String methodB2(){
return varB1;
}
}

final class L extends K{


L(){}
private int varL1;

public void methodL1(int l){


varL1=l;
}
}

class M extends K{
private static char varK1;

M(){}j
public void methodM1(char h){
varK1 = h;
}
}

class DriverProgram{
public static void main(String args[]){
}
}

26) Select from among the following, direct child class/classes of the class J according to the above
declarations.

(a) Class J only (b) Class K only (c) Classes J and K only
(d) Classes L and M only (e) Classes M only

27) Select from among the following, parent class/classes of class M.

(a) Class K only (b) Class M only (c) Classes J and M only
(d) Classes L and K only (e) Class L only

9
28) Select from among the following, illegal class declaration/s which can be seen in the segment of
code.

(a) Class J only (b) Classes J and K only (c) Classes J, K and M only
(d) Class L only (e) All the classes

29) Select from among the following, valid constructor method/s available in the given code.

(a) J(){} (b) K(){} (c) L(){}


(d) N(){} (e) class J

30) Consider the class J and select from among the following, the valid option/s which can be
considered as similar examples for class J.

(a) Employee (b) Tree (c) Vimukthi


(d) United Arab Emirates (e) Coconut Tree

31) Consider the class J and select from among the following, the valid class/classes which can be
considered as similar examples as class K.

(a) Dr. Ruwan Ekanayake (b) CommissionedEmployee (c) PlasticSurgeons


(d) Colombo (e) Dubai

32) Select from among the following, the valid option/s which can be considered as (an) instance
variable(s).

(a) private int varA1; (b) private int varA2;


(c) private String varB1; (d) private static char varK1;
(e) private int varA2;

33) Select from among the following, the valid option/s which can be considered as (a) class variable(s)

(a) private int varA1; (b) private int varA2;


(c) private String varB1; (d) private static char varK1;
(e) private int varA2;

34) Select from among the following, the valid class declaration/s assuming that the body of each class
is not written in the following options.

(a) class H extends J (b) class V extends K


(c) class O extends N (d) class S extends L
(e) final class F extends J

35) Select from among the following, the valid statement/s illustrating object creation.

(a) J obj1= new J(); (b) K obj3 = new K();


(c) K, J obj1, obj2 = new K(), J(); (d) K obj5, obj6= new K();
(e) K obj8 = new K(),J();

10
36) Read the following statement noting the blank indicated as blank.

“The direct two subclasses of the _____blank_____ class are class Error and class Exception.”

Select from among the following, the correct option to fill the blank.

(a) Throwable (b) Object (c) LinkageError


(d) VirtualMachineError (e) ThreadDeath

37) Select from among the following, the package in which the RuntimeException class is defined.

(a) java.lang (b) java.awt (c) javax.swing


(d) java.math (e) java.error

38) Consider the following segment of program written in Java.

int ar[]={1,2,3,4,5,6};
int i= ar.length - 1;

while(i >= 0){


System.out.print(ar[i]);
i--; }

What would the output be, if it is executed as a program?

(a) error (b) 123456 (c) 654321


(d) 21 (e) 6543

39) Consider the following segment of program written in Java.

int ar[]={1,2,3,4,5,6};
int i= ar.length - 1;

while(i >= 0){


if(i < 2) break;
System.out.print(ar[i]);
i--; }

What would the output be, if it is executed as a program?

(a) error (b) 123456 (c) 654321


(d) 21 (e) 6543

40) Consider the following segment of program written in Java.

String str1 = "anuradhapura";


if(str1.endsWith("ra"))
System.out.print(str1);
else
System.out.print("rakwana");

What would the output be, if it is executed as a program?

(a) na (b) ra (c) error


(d) anuradhapura (e) rakwana

11
41) Consider the following segment of program written in Java.

public class Ex41{


public static void main(String args[]){

String names[]={"Kandy","Dubai"};

System.out.print(names);
}
}

Select from among the following, the invalid option/s regarding the above program.

(a) One cannot declare arrays of type String in Java.


(b) It is illegal to declare an array of String without providing the size as an integer value.
(c) The program will not give any compilation errors.
(d) The program will output words Kandy and Dubai when it is executed.
(e) System.out.print(names); is an illegal statement in Java.

42) Consider the following segment of program written in Java.

String str1 = "Vimukthi ";


String str2 = "Jayaweera ";

System.out.print(str1 + str2);

What would the output be, if it is executed as a program?

(a) Vimukthi (b) Vimukthi Jayaweera (c) Jayaweera


(d) Jayaweera Vimukthi (e) error

43) Read the following statement on Streams in Java noting the blank indicated as blank.

“A block of memory that is used to batch up the data that is transferred to or form an external
device, a ___blank______ can be used.”

Select from among the following, the correct option to fill the blank.

(a) CipherInputStream (b) BufferedStream (c) DataInputStream


(d) CheckedInputStream (e) DigestInputStream

44) Select from among the following, direct sub classes which come under InputStream class.

(a) AudioInputStream (b) FileOutputStream (c) ObjectInputStream


(d) FilterInputStream (e) PipedInputStream

45) Select from among the following, type of character set which is supported by Java.

(a) Bytecode (b) Unicode (c) EBSDIC


(d) char (e) String

*********

12

You might also like