Past Papers

You might also like

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

UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY ( EXTERNAL)


Academic Year 2020 – 1st Year Examination – Semester 1

IT1406 – Introduction to Programming


Multiple Choice Question Paper

(TWO HOURS)

Important Instructions :
 The duration of the paper is 2 (two) hours.
 The medium of instruction and questions is English.
 The paper has 40 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.
 Calculators are not allowed.
 All Rights Reserved.

1
1) Select from among the following, the programming language(s) that Java was directly related to during
its development.

(a) C (b) C# (c) C++


(d) F# (e) C--

2) Select from among the following, the valid option(s), that can be considered as keywords of the Java
programming language.

(a) public (b) static (c) void


(d) main (e) String

3) Select from among the following, the valid floating point primitive type(s) available in the Java
programming language.

(a) byte (b) float (c) short


(d) double (e) int

4) Select from among the following, the valid variable initialization(s) allowed in the Java programming
language.

(a) int value1 = 3; (b) byte value2 = 1000; (c) float value3 = 23;
(d) double value4 = 25; (e) char value5 = “ Thisara ”;

5) Select from among the following, the valid option(s) that can be considered as steps in writing a computer
program.

(a) define the problem.


(b) outline the solution.
(c) develop the outline into an algorithm.
(d) test the algorithm for correctness.
(e) document and maintain the program.

6) Select from among the following, the valid features of Object Orientation.

(a) Inheritance (b) Polymorphism (c) Encapsulation


(d) Abstraction (e) Automatic conversion

7) Select from among the following. the valid access control modifier(s) allowed in the Java programming
language.

(a) public (b) private (c) protected


(d) interface (e) implements

2
8) Select from among the following, the package where the Scanner class is stored in Java.

(a) java.util (b) java.applets (c) java.scanner


(d) java.io (e) java.nio

9) Consider the following statements, written about Applets in the Java programming language.

1. They work at client side and so require less response time


2. They are secure
3. They can be executed by browsers running under many platforms, including Linux, Windows,
and Mac OS etc.
4. Plugins are required on the client browser to execute them

Select from among the following, the valid option(s) that can be considered as drawback(s) of Applets in
Java.

(a) Only 1 (b) Only 1, 2 and 3 (c) Only 2 and 4


(d) Only 1 and 4 (e) Only 4

10) Select from among the following, classes which are stored in the java.sql package.

(a) Connection (b) Writer (c) Reader


(d) FileWriter (e) ResultSet

Use the following declarations and initializations to evaluate the Java expressions given in questions
11 - 15. Assume that expressions are evaluated separately in a separate program.

int num1=070,num2=0b00101010;
float x=20.0f;
int var=12_00_000;
char ch='A';//note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the evaluations 11 – 15.

11) System.out.println(x>>1);

(a) 20 (b) 21 (c) 1010


(d) 00001010 (e) error

12) System.out.println(ch>>1);

(a) 1000001 (b) 01000001 (c) 32


(d) 66 (e) error

3
13) System.out.println(num1>2 & num1<35);

(a) 100101 (b) 00100101 (c) true


(d) false (e) error

14) System.out.println(num1 ^ ch);

(a) 01010101 (b) 121 (c) true


(d) false (e) error

System.out.println(num1 >= ++num1);


15)

(a) 20 (b) 01010111 (c) true


(d) false (e) error

16) Consider the following segment of a Java program.

for(int i=0;i<=5;i++){
System.out.print(i);
break;
}

What would the output of this code be?

(a) 0 (b) 012345 (c) 01234


(d) No output (e) error

17) Consider the following segment of a Java program.

float ar[ ]={1,2,3,4,3.4f,4.4f};


for(int i=0;i<6;i++){
System.out.print(ar[ i ]+" ");
}

What would the output of this code be?

(a) 1.0 2.0 3.0 4.0 3.4 4.4 (b) 1 2 3 4 3.4 4.4 (c) 1.0 2.0 3.0 4.0 3.4f 4.4f
(d) 1.0, 2.0, 3.0, 4.0, 3.4, 4.4 (e) error

4
18) Consider the following runtime error generated during an execution of a Java program.

Exception in thread "main" java.lang.NullPointerException

Select from among the following, a valid option of a Java statement by which the above error would
have been generated.

(a) String str=null;


System.out.println(str.length());
(b) String str1="Hiran Thambugala";
int value=Integer.parseInt(str1);
(c) int value1=4,value2=0;
System.out.println(value1/value2);
(d) int ar1[ ]= new int[4];
ar1[4]=4;
(e) int ar1[ 4 ]= new int[ ];
ar1[4]=4;

19) Consider the following runtime error generated during an execution of a Java program.

Exception in thread "main" java.lang.NumberFormatException:

Select from among the following, a valid option of a Java statement by which the above error would have
been generated.

(a) String str=null;


System.out.println(str.length());
(b) String str1="Hiran Thambugala";
int value=Integer.parseInt(str1);
(c) int value1=4,value2=0;
System.out.println(value1/value2);
(d) int ar1[ ]= new int[4];
ar1[4]=4;
(e) int ar1[ 4 ]= new int[ ];
ar1[4]=4;

20) Consider the following expression written in Java.

Integer value=new Integer(20);

Select from among the following a suitable option to identify the process of the above expression.

(a) Auto boxing (b) Boxing (c) in boxing


(d) unboxing (e) auto unboxing

5
21) Consider the following runtime error generated during an execution of a Java program.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

Select from among the following, a valid option of a Java statement by which the above error would have
been generated.

(a) String str=null;


System.out.println(str.length());
(b) String str1="Hiran Thambugala";
int value=Integer.parseInt(str1);
(c) int value1=4,value2=0;
System.out.println(value1/value2);
(d) int ar1[ ]= new int[4];
ar1[4]=4;
(e) int ar1[ 4 ]= new int[ ];
ar1[4]=4;

22) Consider the following program written in Notepad and saved in a working folder called programs.

package MyData;
class MyName{
String name;
MyName(){
name=null;
}
void show(){
System.out.println(name);
}
}
public class DP{
public static void main(String args[]){
MyName ob=new MyName();
ob.show();
}
}

After saving into the program folder and successfully compiling, the java DP command was issued. Select
from among the following the outcome of the program execution.

(a) 0 (b) null (c) name


(d) show (e) error

6
23) Consider the following segment of code written in the Java programming language with a major syntactic
error.

class A {
public interface MyInterface {
boolean isNotNegative(int x); } }
class B implements MyInterface{
}

Select from among the following the code(s) which accounts for this major syntactic error.

(a) class A (b) interface MyInterface (c) isNotNegative


(d) implements MyInterface (e) class B

24) Consider the following segment of a program written in Java noting a/some missing important part(s).

class Gen {
T ob;
Gen(T value) {
ob = value;
}
T getOb() {
return ob;
}

Select from among the following, the acceptable part(s) of the program after introducing the missing
part(s).

(a) class Gen( T ) (b) T getOb( int value) (c) value * ob;
(d) int getOb() (e) class Gen <T>

25) Annotation retention policy determines at what point an annotation is discarded. Which of the following
is/are part of an annotation retention policy?

(a) source (b) interface (c) runtime


(d) retention (e) class

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

String s = "This is a demo of the getChars method.";


int start = 10;
int end = 14;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);

Select from among the following, the output of the program;

(a) This is a demo (b) demo (c) method


(d) start (e) error

7
27) Select from among the following, valid file related methods available in Java.

(a) getName() (b) getPath() (c) canWrite()


(d) canRead() (e) length()

28) Select from among the following, valid byte streams available in Java.

(a) InpurStream (b) OutputStream (c) FileInputStream


(d) FileOutputStream (e) Reader()

Consider the following program written in Java to answer questions 29 – 30.

public class Ex {
public static class Thread1 extends Thread {
public void run() {
System.out.println("A");
System.out.println("B");
}
}
public static class Thread2 extends Thread {
public void run() {
System.out.println("1");
System.out.println("2");
}
}
public static void main(String[] args) {
new Thread1().start();
new Thread2().start();
}
}

29) Select from among the following, similar methods as start() available in Thread class.

(a) sleep() (b) join() (c) yield()


(d) walk() (e) stop()

30) What would the output of the program be when the program executed several times?

(a) 1 2 A B (b) A B 1 2 (c) A 1 B 2


(d) 1 A B 2 (e) error

8
Consider the following table having two columns in it. The first column has line numbers and the
second has different programing statements and expressions. Using this table answer questions 31 –
38 having two parts in it. Part A and Part B. The way that one has to answer these two parts are
different.
Further assume that when answering the questions the following packages are also imported into
the programming environment.
import javax.swing.*; import java.awt.*; import java.awt.event.*;

No. Statements and expressions


1 {
2 }
3 add();
4 add(obj);
5 obj = new JButton("Click");
6 private JButton obj;
7 public class Ex extends JFrame
8 private JLabel text;
9 private JTextField label;
10 text = new JTextField(“Java”);
11 text = new JTextField(8);
12 private JTextField text;
13 label = new JLabel(15);
14 setLayout( new FlowLayout());
15 public Ex()
16 private JLabel label;
17 label = new JLabel("Java");
18 add(label);
19 add(text);

Part A
Answer the questions 31 – 35 by selecting the valid answer(s) based on the table.

31) Select from among the following, valid class names that can be seen in the table given.

(a) JButton (b) JLabel (c) JTextField


(d) text (e) label

32) Select from among the following, valid object names that can be seen in the table given.

(a) JButton (b) JLabel (c) JTextField


(d) text (e) label

33) Select from among the following, valid user defined methods shown in the table given.

(a) add() (b) private (c) public


(d) obj (e) setLayout()

34) Select from among the following, valid access modifiers available in Java.

(a) add() (b) private (c) public


(d) obj (e) setLayout()

9
35) Select from among the following, valid constructor methods illustrated in the table.

(a) JButton (b) JButton() (c) JLabel


(d) JLabel() (e) FlowLayout()

Part B
Questions 36 – 38 give an expected output to be obtained. In these questions, the class declarations
and their main method are also written partially. One is required to select from this table the
relevant statement number(s) required to obtain the expected output.

36) Expected output:

public class Ex extends JFrame{


(a) 1,13,14,15,16,17,2 (b) 16,15,1,14,17,18,2 (c) 1,13,4,5,6,7.9,2
(d) 2,3,5,6,8,9,13.1 (e) 1,3,5,7,8 9
public static void main(String[] args) {
Ex gui= new Ex();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(100, 100);
gui.setVisible(true);
}
}

37) Expected output:

public class Ex extends JFrame{


(a) 12,15,1,14,10,18,2 (b) 10,1,7,5,6,7,8,2 (c) 5,6,7,1,3,4,6,2
(d) 1,11,7,5,6,3,8,2 (e) 12,15,1,14,11,19,2
public static void main(String[] args) {
Ex gui= new Ex();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(100, 100);
gui.setVisible(true);
}
}

10
38) Expected output:

public class Ex extends JFrame{


(a) 7,15,1,14,4,5,2 (b) 6,15,1,14,5,4,2 (c) 7,8,1,13,4,6,2
(d) 8,7,1,14,4,5,2 (e) 9,7,1,14,5,4,2
public static void main(String[] args) {
Ex gui= new Ex();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(100, 100);
gui.setVisible(true);
}
}

39) Consider the following program written in the Java programming language.

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class Main extends JFrame {
JCheckBox check = new JCheckBox("Checkbox", false);
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
check.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println("Checked? " + check.isSelected());
}
});
getContentPane().add(check);
pack();
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}

Select from among the following, the changes required to the program for event handling in Java, if the
software engineer uses a button instead of a check box in the program.

(a) addItemListener  addMouseListener, ItemEvent  MouseEvent


(b) addItemListener  addWindowEventListerner, ItemEvent  WindowEvent
(c) addItemListener  addActionListener, ItemEvent  ActionEvent
(d) addItemListener  addTextEvenListener, ItemEvent  TextEvent
(e) addItemListener  addFocusListener, ItemEvent  FocusEvent

11
40) Select from among the following, valid method(s) which are related to the JFrame class.

(a) setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
(b) setVisible(true);
(c) setTitle(“Play a game”)
(d) setText(“Welcome”);
(e) setSize(200,300);

*****

12
Bachelor of Information Technology
Academic Year 2020– 1st Year Examination – Semester 1
IT1406 – Answers

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


Academic Year 2019 – 1st Year Examination – Semester 2

IT2205 - Programming I
02nd November, 2019
(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 11 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
auroracs.lk
Consider the following program to answer questions 1 – 4.

public class Ex1{


public static void main (String args[]){
System.out.println("Good Luck");
}
}

1) Select from among the following keyword(s) that can be seen in the program.

(a) class (b) static (c) void


(d) public (e) main

2) Select from among the following valid file name(s) to save the above program.

(a) Ex1.java (b) class.java (c) java.java


(d) Ex1.java.txt (e) Ex1.txt.java

3) Select from among the following valid command(s) to compile the program.

(a) Java Ex1.java (b) javac Ex1 (c) javacompile


(d) compile (e) javaCompile

4) Select from among the following valid option(s) that can be used to execute the Java
program.

(a) java Ex1.java (b) java Ex1 (c) class


(d) javajava (e) Ex1.txt.java

Consider the following program to answer questions 5 – 6.

public class Ex5{


public static void main (String args[]){
System.out.print("1");
System.out.print("2");
**/
System.out.print("3");
System.out.print("4");
System.out.print("5");
/
}
}

5) What would the output of this program be?

(a) 12345 (b) 1234 (c) 345


(d) 12 (e) error

6) Select from among the following, valid comments in the Java programming language.

(a) // (b) **/ / (c) /* */


(d) */ /* (e) \\

2
auroracs.lk
7) Select from among the following, correct option(s) which illustrate(s) primitive data types in Java.

(a) byte (b) char (c) String


(d) Integer (e) Math

8) Select from among the following, correct option(s) which illustrate(s) reference data types in Java.

(a) byte (b) char (c) String


(d) Integer (e) System

Consider the following program to answer questions 9 – 10.

public class Ex9{


public static void main (String args[]){
byte b;
int i=258;
double d=325.59;
b=(byte)i;
System.out.print(b+" ");
i=(int)d;
System.out.print(i+" ");
b=(byte)d;
System.out.print(b+" ");
}
}

9) What would the output of this code be?

(a) 258 325 325 (b) 2 325 59 (c) 2 325 69


(d) 25 325 325 (e) error

10) Consider the following sample expressions available in the program.

b=(byte)i;
i=(int)d;
b=(byte)d;

Select from among the following, correct option(s) to name the above conversion process practiced
in the expressions.

(a) automatic conversion (b) comparison (c) casting


(d) saving (e) debugging

11) Select from among the following, valid variable names in Java.

(a) number (b) firstName (c) _notation


(d) $value (e) STATIC

12) Select from among the following, valid logical operators in Java.

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


(d) || (e) !

3
auroracs.lk
13) Consider the following program written in Java.

public class Ex10{


public static void main (String args[]){
int radius=3;
float areaOfCircle=Math.PI*radius*radius;
System.out.println(areaOfCircle);
}
}

What would the output of this code be?

(a) 28 (b) 28.344444 (c) 28.34


(d) 28.34445 (e) error

Consider the following program to answer questions 14 – 15 noting the blank1, blank2 and
blank3.

public class Ex14{


public static void main (String args[]){
System.out.println("Programming blank1 isblank2 fun blank3");
}
}

14) When the program is executed successfully, the following output is displayed on the console.

Programming
i fun"

Select from among the following, valid literals to fill the blanks.

(a) blank1  // blank2  /* blank3  \\


(b) blank1  // blank2  /* */ blank3  &
(c) blank1  \\ blank2  \\\\ blank3  \\\\\
(d) blank1  \n blank2  \b blank3  \”
(e) blank1  \n blank2  \t blank3  \\

15) Select from among the following, the valid name(s) for the selected literal category.

(a) Arithmetic (b) logical (c) Escape sequences


(d) ASCII (e) Bitwise

16) Consider the following sentence in English noting the blank.

Java is a ------------blank--------------programming language;

Select from among the following the correct option(s) to fill the blank in the sentence.

(a) object oriented


(b) platform independent
(c) case sensitive
(d) weakly typed
(e) multithreaded

4
auroracs.lk
Use the following declarations and initializations to evaluate the Java expressions given in
questions 17 - 22. Assume that expressions are evaluated separately in a separate program.

int num1=070,num2=0b00101010;
float x=20.0f;
int var=12_00_000;
char ch='A';//note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the evaluations 17 – 22.

17) System.out.println(var);

(a) 12_00_000 (b) 12,00,000 (c) 12-00-000


(d) 1200000 (e) error

18) System.out.println(num1);

(a) 070 (b) 70 (c) 07


(d) 56 (e) error

19) System.out.println(num2);

(a) 0b00101010 (b) 0b (c) 00101010


(d) 42 (e) error

20) System.out.println(x+var);

(a) 1200020.0 (b) 12,00,020.0 (c) 12,00,020


(d) 12_00_020_0 (e) error

21) System.out.println(x>num1);

(a) false (b) true (c) 62.0f


(d) 62.0 (e) error

22) System.out.println(ch+x);

(a) A+20.0f (b) A20 (c) 85.0


(d) A20.0 (e) error

23) Select from among the following, valid exception class(es) which is/are referenced when a division
by zero error occurs.

(a) NumberFormatException
(b) ArithmaticException
(c) IOException
(d) NullPointerException
(e) ArrayIndexOutOfBoundException

5
auroracs.lk
24) Select from among the following, valid exception class(es) which can be categorized as unchecked
exceptions.

(a) NumberFormatException
(b) ArithmaticException
(c) IOException
(d) NullPointerException
(e) ArrayIndexOutOfBoundException

25) Select from among the following, valid exception class(es) which can be categorized as checked
exceptions.

(a) NumberFormatException
(b) ArithmaticException
(c) IOException
(d) NullPointerException
(e) ArrayIndexOutOfBoundException

Consider the following program to answer questions 26 – 27.

public class Ex25{


public static void main (String args[]){
try {
nt a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}

26) Select from among the given options, the correct output of the program.

(a) 0 (b) 5 (c) A


(d) B (e) error

27) Select from among the following, the valid option(s) that can be categorized under the key words try
and catch which are used for exception handling in Java.

(a) throw (b) throws (c) Scanner


(d) finally (e) number1

6
auroracs.lk
Consider the following table having two columns in it. The first column has line numbers and
the second has different programing statements and expressions. Using this table answer
questions 28 – 31.
Each question gives an expected output to be obtained. It also gives the class declarations and
its main method. One is required to select from this table the relevant statement number(s)
required to obtain the expected output.

No. Statements and expressions


1 {
2 }
3 for(int i=0;i<=3;i++)
4 for(int i=0;i<4;i++)
5 for(int j=0;j<=4;j++)
6 for(int j=0;j<=3;j++)
7 System.out.println("*");
8 System.out.print("*");
9 System.out.print();
10 System.out.println();
11 if( i != j )
12 if( i == j )
13 else
14 System.out.print(“A”);
15 System.out.println(“A”);
16 System.out.print(“ “);
17 if( i+j == 30 )
18 import java.java.*;
19 if( i = j )
20 else( i = j )
21 for( j=0;j<=3;j++)
22 if( i = j ):
23 for(int j=0,j<=,j++)
24 elif
25 if( i+j == 3 )
26 >>
27 <<
28 if( i+3 = 3 )

28) Expected output:


****
****
****
****

Select from among the following, the correct statement number sequence(s) to obtain the pattern
shown above.
public class Ex28{
public static void main(String args[]){
(a) 6,1,4,8 (b) 3,1,6,8 (c) 6,1,5,8,9,23
(d) 4,1,6,8 (e) 6,1,6,8,23,2
System.out.println();
}
}
}

7
auroracs.lk
29) Expected output:
A***
*A**
**A*
***A
Select from among the following, the correct statement number sequence(s) to obtain the pattern
shown above.
public class Ex29{
public static void main(String args[]){
(a) 6,1,4,11,8,13,14 (b) 6,1,6,11,8,,13,14 (c) 4,1,6,11,8,13,14
(d) 6,1,5,11,8,13,14,9,2 (e) 3,1,6,11,8,13,14
System.out.println();
}
}
}

30) Expected output:


AAA
A AA
AA A
AAA
Select from among the following, the correct statement number sequence(s) to obtain the pattern
shown above.

public class Ex30{


public static void main(String args[]){
(a) 6,1,4,11,14,13,16 (b) 4,1,6,11,14,13,16 (c) 3,2,6,1,14,13,14,10,1
(d) 3,1,6,11,14,13,16 (e) 6,1,4,11,16,13,14
System.out.println();
}
}
}

31) After seeing the pattern shown in question number 30), the software engineer changed the coding of
the program to obtain the following pattern.
A
A
A
A
Select from among the following, the correct statement number sequence(s)to obtain the pattern
shown above.
public class Ex31{
public static void main(String args[]){
(a) 6,1,4,25,14,13,16 (b) 27,1,4,2,9,18,27 (c) 7,1,8,9,2,11,25,26,27,2
(d) 7,2,8,8,2,10 (e) 6,1,9,17,18,1
System.out.println();
}
}
}

8
auroracs.lk
32) Select from among the following, correct option(s) where the applet class is located.

(a) java.applet (b) java.io (c) java.awt


(d) java.lang (e) java.sql

33) Select from among the following, the sub class(es) of the Reader class.

(a) FileReader (b) FileWriter (c) ClassReader


(d) ClassWriter (e) InputReader

34) Consider the following program written in Java.

public class Ex34{


public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i <= arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}

Select from among the following the output of the program.

(a) 10 (b) 10 20 (c) 10 20 30 40


(d) 10 20 30 40 50 (e) error

35) Consider the following program written in Java.


class Ex35 {
public static void main(String args[]){
int ar[]=new int[-1];
System.out.println(ar.length);
}
}

What would the output of the program be?

(a) -1 (b) -2 (c) 1


(d) 0 (e) error

Consider the following program to answer questions 36 – 37.

class Car{
private int carNo;
class Engine{
int engineNo;
}
}
class Ex36 {
public static void main(String args[]){
Car obj1=new Car();
Car.Engine obj2=obj1.new Engine();
System.out.println(obj2.engineNo);
}
}

9
auroracs.lk
36) What would the output of the program be?

(a) 3 (b) 2 (c) 1


(d) 0 (e) error

37) Select from among the following, name(s) of (the) byte code(s) which has/have been generated after
compiling the program successfully.

(a) Car.class (b) Car$Engine.class (c) Ex36.class


(d) Engine$Car.class (e) Ex36$Car.class

Consider the following diagram to answer for questions 38 - 42.

Employee

Security Manager Clerk Driver

38) Select from among the following, the valid option(s) that can be considered as super classes in the
given diagram.

(a) Employee (b) Manager (c) Clerk


(d) Driver (e) Security

39) Select from among the following, the valid option(s) that can be considered as child classes in the
given diagram.

(a) Employee (b) Manager (c) Clerk


(d) Driver (e) Security

40) Select from among the following, valid option(s) where one can apply the key word abstract during
class declaration considering the given scenario.

(a) Employee (b) Manager (c) Clerk


(d) Driver (e) Security

41) Select from among the following, valid option(s) where one can apply the key word final during class
declaration considering the given scenario.

(a) Employee (b) Manager (c) Clerk


(d) Driver (e) Security

42) A software engineer has written the following method signatures in the class Clerk.
Clerk()
Clerk(String name, String address)

Select from among the following suitable name/s to identify the methods.

(a) Instance variables (b) Instance methods (c) class variables


(d) class methods (e) constructors

10
auroracs.lk
43) Consider the following partially written programs in Java.
class A{ }
class B { }
interface In1{}
Interface In2{}
Interface In3{}
Interface In4{}

Select from among the following the valid programming statement(s) in Java.

(a) interface In5 extends In4,In1{ }


(b) class C extends A,B{ }
(c) class D extends In1{ }
(d) interface In6 implements B{ }
(e) class E implements In1,In3{ }

Consider the following program to answer questions 44 – 45.


44) Consider the following program written in Java.
class Grandparent {
public void Print() {
System.out.println("Grandparent");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent");
}
}
class Child extends Parent {
public void Print() {
super.super.Print();
System.out.println("Child");
}
}
public class Ex45{
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

What would the output of the program be?

(a) Grandparent (b) Parent (c) Child


(d) c.Print(); (e) error

45) Select from among the following, the valid object oriented feature(s) used in the program.

(a) data abstraction (b) inheritance (c) method overriding


(d) polymorphism (e) database access

*******

11
auroracs.lk
Bachelor of Information Technology
Academic Year 2019 – 1st Year Examination – Semester 2
IT2205 Programming 1 – Answers

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


Academic Year 2018 – 1st Year Examination – Semester 2

IT2205 - Programming I
22nd September, 2018
(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 13 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.
 Calculators are not allowed.

1
auroracs.lk
1) Which of the following are keywords in Java?

(a) public (b) static (c) void


(d) int (e) try

2) Consider the following expression in the Java programming language.

BLANK number = 5;

Select valid option(s) to replace the term BLANK in this statement, from among the
following.

(a) byte (b) short (c) int


(d) long (e) float

3) Consider the following code written in Java.

public class Ex3{


public static void main(String args[]){
int value;
System.out.print(value);
}
}

What would the output of this code be?

(a) 0 (b) value (c) hex value of a memory


location
(d) 7 (e) error

4) Consider the following code written in Java.

public class Ex3{


public static void main(String args[]){
final int value;
value = 7;

System.out.print(value);
}
}

What would the output of this code be?

(a) 0 (b) value (c) 6


(d) 7 (e) error

2
auroracs.lk
5) Consider the following code written in Java.

public class Ex3{


public static void main(String args[]){
final int value;
value = 7;
System.out.print(value);
value = 7;
System.out.print(value);
}
}

What would the output of this code be?

(a) 0 (b) value (c) 6


(d) 77 (e) error

6) Select from among the following, those which can be considered as comments allowed in Java.

(a) //comment (b) \\ comment (c) /* comment */


(d) \**comment *\ (e) #

7) Select from among the following, correct option(s) which illustrates escape sequence(s) allowed in
Java.

(a) \’ (b) \” (c) \t


(d) \n (e) \b

8) Select from among the following, the default character encoding used in Java programming language.

(a) ASCII (b) UNICODE (c) ISO-LATIN-1


(d) EBCDIC (e) char

9) Consider the following code written in Java.

enum Colour
{
RED, GREEN, BLUE;
}

public class Ex9{


public static void main(String[] args){
Colour c1=Colour.RED;
System.out.println(c1);
}
}

What would the output of this code be?

(a) 0 (b) 1 (c) RED


(d) 3 (e) error

3
auroracs.lk
10) Consider the following code written in Java.

public class Ex10 {


public static void main(String[] args) {
char a ='\\';
System.out.println("C:"+a+"TC"+a+"BIN");
}
}

What would the output of this code be?

(a) C:\TC\BIN (b) C:aTCaBIN (c) C:\\TC\\BIN


(d) "C:"+a+"TC"+a+"BIN" (e) error

11) Consider the following code written in Java.

class Ex11 {
public static void main(String args[]){
double a,b,c;
a = 3.0/0;
b = 0/4.0;
c = 0/0.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}

What would the output of this code be?

(a) infinity,0.0,NaN (b) 0.0,0.0,0.0 (c) NaN,3,4


(d) 7,7,7 (e) 3,4,0

Use the following declarations and initializations to evaluate the Java expressions given in
questions 12 - 17. Assume that each expression is evaluated separately in the program.
int a=-1,b=2;
float x=10.0f;
char ch='A';//note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the evaluations 12 – 17.

12) System.out.println(a<<<1);

(a) -1 (b) 1 (c) 001


(d) 100 (e) error

13) System.out.println(b>>>1);

(a) 1 (b) 2 (c) 3


(d) 01 (e) error

14) System.out.println(x>>1);

(a) false (b) true (c) 1


(d) 01 (e) error

4
auroracs.lk
15) System.out.println(x<<1);

(a) false (b) true (c) 01


(d) 1 (e) error

16) System.out.println(ch<<1);

(a) false (b) true (c) 130


(d) 67 (e) error

17) System.out.println(ch>>1);

(a) 130 (b) 32 (c) 66


(d) 67 (e) error

Consider the following table having two columns in it. First column has line numbers and the
second has different programing statements and expressions. Using this table answer questions
18 – 22. Each question gives an expected output to be obtained. It also gives the class
declarations and its main method. You are required to select from this table the relevant
statement number(s) required to obtain the expected output.

No. Supportive components for program writing


1 {
2 }
3 for(int j=0;j<=6;j++)
4 System.out.println("* ");
5 System.out.print("* ");
6 for(int j=1;j<=6;j++)
7 for(int j=0;j<=16;j++);
8 for(int j=1;j==6;j++)
9 for(int k=1;k<=6;k++)
10 System.out.println();
11 for(int k=j;k<=6;k++)
12 if(j==k)
13 else
14 System.out.print(" ");
15 System.out.print();
18) Expected output:

*
*
*
*
*
*
*

Select from among the following, the correct option(s) to obtain the pattern shown above.

5
auroracs.lk
public class Ex18{
public static void main(String args[]){
(a) 1,3,1,2,4,2 (b) 3,4 (c) 3,5
(d) 8,4 (e) 3,1,4,2
}
}

19) Expected output:

******

Select from among the following, the correct option(s) to obtain the pattern shown above.

public class Ex19{


public static void main(String args[]){
(a) 1,3,1,2,4,2 (b) 3,4 (c) 6,5
(d) 8,4 (e) 3,1,6,2
}
}

20) Expected output:

******
******
******
******
******
******

Select from among the following, the correct option(s) to obtain the pattern shown above.

public class Ex20{


public static void main(String args[]){
(a) 6,1,9,5,10,2 (b) 3,1,5,2,11 (c) 7,1,8,9,2,10
(d) 7,1,8,8,2,10 (e) 6,1,9,10,5,2
}
}

21) After seeing the pattern shown in question number 20), the software engineer changed the coding of
the program to obtain the following pattern.

******
*****
****
***
**
*

Select from among the following, the correct option(s) to obtain the pattern shown above.

6
auroracs.lk
public class Ex21{
public static void main(String args[]){
(a) 6,1,11,5,10,2 (b) 3,1,4,2,9 (c) 7,1,8,9,2,11
(d) 7,2,8,8,2,10 (e) 6,1,9,10,5,2
}
}

22) After seeing the pattern shown in the question number 20), the software engineer changed the coding
of the program to obtain the following pattern.

*
*
*
*
*
*

Select from among the following, the correct option(s) to obtain the pattern shown above.

public class Ex22{


public static void main(String args[]){
(a) 6,1,9,12,5,13,14,10,2 (b) 3,1,4,2,9,13,14,10,2 (c) 7,1,8,9,2,11,14,12,15
(d) 7,2,8,8,2,10,2 (e) 6,1,9,10,5,2,14
}
}

23) In Java programming, an array is represented as:

(a) an object (b) a key word (c) an operator


(d) a character literal (e) an escape sequence

24) Consider the following program written in Java.

class Ex24 {
public static void main(String args[]){
int ar[]=new int[2];
System.out.println(ar[0]+ar[1]);
}
}

What would the output of the program be?

(a) 5 (b) 3 (c) 1


(d) 0 (e) error

7
auroracs.lk
25) Consider the following program written in Java.

class Ex25 {
public static void main(String args[]){
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}

What would the output of the program be?

(a) 45 (b) 3 (c) 1


(d) 0 (e) error

26) Consider the following program written in Java.

class Ex26 {
public static void main(String args[]){
int ar1[] = new int[] {0,1, 2, 3};
System.out.println(ar1);
}
}

What would the output of the program be?

(a) 6 (b) 3 (c) 1


(d) Some garbage value (e) error

27) Consider the following program written in Java.

class Ex27 {
public static void main(String args[]){
int ar1[][] = {{ 1, 2, 3},
{ 4 , 5, 6},
{ 7, 8, 9}};
int sum = 0;

for (int i = 0; i < 3; ++i)


for (int j = 0; j < 3 ; ++j)
sum = sum + ar1[i][j];
System.out.print(sum / 5);
}
}

What would the output of the program be?

(a) 6 (b) 7 (c) 45


(d) 9 (e) error

8
auroracs.lk
28) Select from among the following, the valid option(s) that can be used for naming classes, variables
or methods in the Java programming language.

(a) keywords (b) operators (c) identifiers


(d) separators (e) constants

29) A process that involves recognizing and focusing on the important characteristics of a situation or
object, as an object oriented feature is known as:

(a) Encapsulation (b) Polymorphism (c) Inheritance


(d) Abstraction (e) Data hiding

30) In object oriented programming new classes can be defined by extending existing classes. In object
orientation, this is referred to as:

(a) Encapsulation (b) Polymorphism (c) Inheritance


(d) Abstraction (e) Data hiding

31) Select from among the following, correct object oriented feature which describes the process of
wrapping up of data and functions into a single unit.

(a) Encapsulation (b) Polymorphism (c) Inheritance


(d) Abstraction (e) Data hiding

32) Select from among the following, the valid object oriented feature(s) which show(s) the ability of
two or more objects belonging to different classes to respond to exactly the same method in different
classes.

(a) Encapsulation (b) Polymorphism (c) Inheritance


(d) Abstraction (e) Data hiding

33) Consider the following program written in Java.


public class Ex33{

static int i = 1;

public static void main(String args[]){


System.out.print(i+" , ");
m();
m();
System.out.println(i);
}

public static void m(){


i += 2;
}
}

What would the output of the program be?

(a) 1 , 1 (b) 1 , 3 (c) 1 , 4


(d) 1 , 5 (e) error

9
auroracs.lk
34) Consider the following program written in Java.
public class Ex34{

static int i = 1;

public static void main(String args[]){


System.out.print(i+" , ");
m(i);
m(i);
System.out.println(i);
}

public static void m(int i)


{
i += 2;
}
}

What would the output of the program be?

(a) 1 , 1 (b) 1 , 3 (c) 1 , 4


(d) 1 , 5 (e) error

35) Consider the following program written in Java.

class Test{
void method(int i, int j){
i *= 2;
j /= 2;
}
}

class Ex35{
public static void main(String args[]){
Test ob = new Test();
int a = 15, b = 20;
System.out.println(a +" " + b);
ob.method(a,b);
System.out.println(a + " " +b);
}
}

What would the argument passing method be which is used by the above program?

(a) Call by value (b) parameter (c) Call by reference


(d) println() (e) method()

36) Select from among the following, the valid option(s) to which the @Deprecated annotation can be
applied.

(a) methods (b) variables (c) operators


(d) identifiers (e) arrays

10
auroracs.lk
37) Consider the following program written in Java.

class A{
protected int a;
A(){a=7;}
}

class B extends A{
private int b;
B(){b=3;}
public void show(){
System.out.println(a+" "+b);
}
}

public class Ex37 {


public static void main(String args[]) {
B obj=new B();
obj.show();
}
}

What would the output of the program be?

(a) 7 3 (b) 10 (c) 3 7


(d) 4 (e) error

Consider the following program to answer questions 38 – 40.

interface Calculate{
void cal(int item);
}

class Display blank1 Calculate{


int x;
public void cal(int item){
x = item * item;
}
}

class Ex40{
public static void main(String args[]){
Display arr = new Display();
arr.x = 0;
arr.cal(2);
System.out.print(arr.x);
}
}

38) Select from among the following, the valid option(s) to fill blank1.

(a) extends (b) extend (c) implements


(d) implement (e) abstract

11
auroracs.lk
39) Consider the following statement written in the above program.

void cal(int item);

Select from among the following, invalid alternative way(s) of implementing the above statement in
an interface.

(a) private abstract void cal(int item);


(b) public abstract void cal(int item);
(c) private void abstract cal(int item);
(d) public void abstract cal(int item);
(e) private abstract void cal();

40) What would the output of the program be, if the software engineer has filled the blank1 with the
right option in a syntactically correct manner?

(a) 1 (b) 2 (c) 3


(d) 4 (e) error

41) Consider the following program written in Java.

class Ex41{
public static void main(String args[]){
String obj = "ILIKEJAVA";
System.out.println(obj.charAt(7));
}
}

What would the output of the program be?

(a) I (b) L (c) V


(d) A (e) error

42) Consider the following program written in Java.

class Ex42{
public static void main(String args[]){
String obj = "ILIKEJAVA";
System.out.println(obj.length());
}
}

What would the output of the program be?

(a) 9 (b) 10 (c) 11


(d) 8 (e) error

12
auroracs.lk
43) Consider the following program written in Java.

class Ex4{
public static void main(String args[]){
String s1 = "Hello";
String s2 = s1.replace('l','w');
System.out.println(s2);
}
}
What would the output of the program be?

(a) ll (b) ww (c) Hello


(d) Hewwo (e) error

44) Select from among the following, valid interface(s) in the collection framework, which must contain
unique elements in it.

(a) Integer (b) Set (c) List


(d) Array (e) Collection

45) Select from among the following, the valid keyword(s) used for exception handling in Java.

(a) try (b) catch (c) throw


(d) throws (e) finally

*******

13
auroracs.lk
Bachelor of Information Technology
Academic Year 2018 – 1st Year Examination – Semester 2
IT2205 – Answers

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


Academic Year 2017 – 1st Year Examination – Semester 2

IT2205 - Programming I
11th November, 2017
(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 11 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
auroracs.lk
1) Select from among the following, correct options which can be considered as logical
operators in Java.

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


(d) && (e) !

2) Java programming language is a language which is

(a) case sensitive. (b) object oriented.


(c) signed. (d) multithreaded.
(e) platform independent.

Consider the following program written in Java to answer for questions 3 - 4.

public class Ex2 {

public static void main(String args[]){


byte num;
int value = 258;
double no = 325.59;

num = (byte) value;


System.out.print(num);

value = (int) no;


System.out.print(value);

}
}

3) What would the output of the program be?

(a) 2325 (b) 2323


(c) 2523 (d) 2222
(e) 7878

4) Select from among the following, the valid option which explains the conversion occur here
in the program.

(a) Automatic conversion (b) casting


(c) polymorphism (d) assignment
(e) comparing

5) In the beginning, Java was created in order to

(a) connect many household machines. (b) perform operations on the Internet.
(c) create high performance OS. (d) send the path finder to the mars.
(e) compete with Android.

2
auroracs.lk
6) Select from among the following, those which cannot be considered as primitive data types in Java.

(a) main (b) void (c) float


(d) static (e) boolean

7) Select from among the following, correct option(s) which illustrate/s correct long type literals in Java.

(a) 0x99fffL (b) ABCDEFG (c) 0x99fffa


(d) 99671246 (e) 132426677L

8) Consider the following program written in Java.

public class Ex8{


public static void main(String args[]){
System.out.println(“In first main()”);
}
public static void main(int args[]){
System.out.println(“a”);
}
}

What would the output of the program be?

(a) Code will not compile and will give "Duplicate main() method declaration" error.
(b) Code will compile correctly but will give a runtime exception.
(c) Code will compile correctly and will print "In first main()" (without quotes) when it is
run.
(d) Code will compile correctly and will print "a" (without quotes) when it is run.
(e) Source code cannot be saved since there are two main methods in the program.

9) Default access modifier is available to

(a) all the packages. (b) all the methods. (c) only within the package.
(d) only within the class. (e) all the systems.

10) Consider the following program written in Java.

public class Ex10{


public static void main(String args[]) {
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
System.out.println(sum);
}
}

What would the output of the program be?

(a) 10 (b) 20 (c) 30


(d) 40 (e) 50

3
auroracs.lk
11) Select from among the following, the operator(s) used to invert all the digits in a binary representation
number.

(a) ~ (b) >> (c) <


(d) ! (e) &

Use the following declarations and initializations to evaluate the Java expressions given in
questions 12 - 17. Assume that each expression is evaluated separately in the program.

float value1 = 2.0f;


int num1 = 10;
String str = "100";
char ch = 'B'; // note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the evaluations 12 – 17.

12) System.out.println(str+ch);

(a) 165 (b) 100A (c) 166


(d) 100B (e) error

13) System.out.println(ch+value1);

(a) 65.0 (b) 65 (c) 68.0


(d) 68 (e) error

14) System.out.println(ch<str);

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


(d) 66 (e) error

15) System.out.println(num1 > value1*5);

(a) false (b) true (c) 76


(d) 75 (e) error

16) System.out.println(str++);

(a) false (b) true (c) 100


(d) 101 (e) error

17) System.out.println(!(num1+ch));

(a) false (b) true (c) 66


(d) 75 (e) error

18) Select from among the following, the key word(s) which is/are not a part of exception handling in
Java programming language.

(a) try (b) catch (c) thrown


(d) finally (e) final

4
auroracs.lk
19) Consider the following program written in Java.

public class Ex19 {


public static void main(String args[]) {
try {
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
sum = (sum / i);

}
catch(ArithmeticException e) {
System.out.print("0");
}
System.out.print(sum);
}
}

What would the output of the program be?

(a) Run time error (b) 0 (c) 05


(d) Compile time error (e) 005

20) Select from among the following, (a) valid key word(s) which prevent(s) overriding methods in the
super class.

(a) final (b) public (c) static


(d) void (e) abstract

21) What will be the version which is referred, when an overridden method is called from within a
subclass?

(a) super class


(b) sub class
(c) compiler will choose the method
(d) interpreter will choose the method
(e) the programmer will decide the particular method

Applicants are advised to read questions 22 – 27 keeping in mind that these questions have
connections to another.

22) Consider the following segment of Java class declaration.

public abstract class Employee{


}

Select from among the following, the valid statement(s) allowed within the class Employee.

(a) private int age;


(b) public void setAge(int age){this.age=age;}
(c) public int getAge(){return age;}
(d) public abstract void earn();
(e) Employee(){}

5
auroracs.lk
23) Consider the following segment of Java class declaration.

public class Teacher extends Employee{


}

Select from among the following, valid option(s) that must be selected in the class declaration which
is required by the compiler.

(a) private String name;


(b) public void setName(String name){this.name=name;}
(c) public String getName(){return name;}
(d) public void earn(){return monthlySalary};
(e) Teacher(){}

24) Select from among the following, valid option(s), which can be considered logically within the class
declaration Teacher which is a sub class of the Employee class. Even though the given programming
statement(s) are correct one has to consider the suitability of them concerning Object oriented
features.

(a) private String name;


(b) public void setName(String name){this.name=name;}
(c) public String getName(){return name;}
(d) public void earn(){return monthlySalary};
(e) Teacher(){}

25) Consider the following segment of class declaration.

public final class Clerk extends Employee(){}

Subsequently consider the following class declaration also

public class PayrollClerk extends Clerk(){}

After that one has created an object of the class PayrollClerk as follows.

PayrollClerk obj1= new PayrollClerk();

At the time of compiling, an error was generated. Select from among the following, valid option(s)
which shows the most close reason(s) for the error generated.

(a) new (b) final (c) class


(d) extends (e) obj1

26) One has made the following class declaration.

public class PartTimeTeacher extends Teacher,Clerk{ }

Select from among the following, the invalid object oriented feature, which has been tried to
introduce in the above class declaration.

(a) Data abstraction (b) Information hiding (c) Encapsulation


(d) Multiple inheritance (e) Polymorphism

6
auroracs.lk
27) Select from among the following, the valid object declaration(s) which is/are valid in Java
programming language.

(a) Clerk obj1= new Clerk();


(b) Teacher obj2= new Teacher();
(c) Employee obj3= new Employee();
(d) PartTimeTeacher new = new PartTimeTeacher();
(e) Employee obj5= new Teacher();

28) Select from among the following conditions that are not allowed in Java programming language
assuming var1, var2 and var3 variables are declared correctly in the program.

(a) var1 == var2 (b) var1 =< var3 (c) var1 > var2 > var3
(d) var2 => var3 (e) var1++

29) Consider the following program written in Java.

public class Ex29 {


public static void main(String[] args) {
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}

What would the output of the program be?

(a) 4 (b) 5 (c) 6


(d) 7 (e) error

30) Consider the following program written in Java.


public class Ex30 {
public static void main(String[] args) {
int number1 = 5;
int number2 = 10;
first: {
second: {
third: {
if (number1 == number2 >> 1)
break second;
}
System.out.println(number1);
}
System.out.println(number2);

} } }

What would the output of the program be?

(a) 7 (b) 8 (c) 9


(d) 10 (e) error

7
auroracs.lk
31) Consider the following program written in Java.
class Ex31{
public static void main(String args[]){
String s1 = "Hello i love java";
String s2 = new String(s1);
System.out.println((s1 == s2) + " " + s1.equals(s2));
}
}

What would the output of the program be?

(a) false true (b) 18 (c) 16


(d) true false (e) error

32) Consider the following program written in Java.


class Ex32{
public static void main(String args[]){
int x=7;
if(x==2); // Note the semicolon
System.out.print("Manuja ");
System.out.print("Gunasena ");
}
}

What would the output of the program be?

(a) Manuja (b) Gunasena (c) 7


(d) Manuja Gunasena (e) error

33) Consider the following program written in Java.


public class Ex33{
public static void main(String[] args){
int x = 3, result = 4;
switch(x + 3){
case 6: result = 0;
case 7: result = 1;
default: result += 1;
}
System.out.println(result);
}
}

What would the output of the program be?

(a) 2 (b) 3 (c) 4


(d) 6 (e) error

8
auroracs.lk
34) Consider the following program written in Java.

public class Ex34{


public static void main(String args[]){
int a, b=1;
a = (b>1)?2:1;
switch(a){
case 0: System.out.println(0); break;
case 1: System.out.println(1);
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
}

What would the output of the program be?

(a) 1 (b) 2 (c) 4


(d) 3 (e) error

35) Select from among the following, the valid key word(s) which is/are used by an interface to use
another interface which is defined previously.

(a) interface (b) extends (c) implements


(d) try (e) class

36) Select from among the following, (a) valid abstract method declaration/s in an abstract class.

(a) public abstract void method1(){}


(b) public void abstract method1();
(c) public abstract void method1();
(d) public void method1();
(e) public abstract method1();

37) Select from among the following, the correct method to display a string in an applet.

(a) display() (b) print() (c) println()


(d) drawstring() (e) String()

9
auroracs.lk
Consider the following program written in Java to answer questions 38 – 39.

class Test {
public static void main(String[] args) {
String chars[] = { "A", "B"};

for (int sample: chars) {


System.out.print(sample);
}
}
}

38) What would the output of the program be?

(a) 6566 (b) “A” “B” (c) languages


(d) int (e) error

39) Assume that one needs to get the output of the program as A and B (A B). Select from among the
following, the programing statement(s) which is/are needed to be changed.

(a) for (int sample: chars) (b) class Test (c) String chars[]
(d) System.out.print(sample); (e) { "A", "B"};

40) Select from among the following, the valid option(s) which store(s) all the standard classes like
String, System in Java.

(a) applet (b) io (c) lang


(d) standard (e) util

41) Select from among the following, valid escape sequences in Java.

(a) \\ (b) // (c) /n


(d) \r (e) \”

42) Select from among the following, (a) valid option(s) which is/are not interfaces in Java collection
framework.

(a) Collection (b) Set (c) List


(d) Group (e) Map

43) Select from among the following, the valid definition(s) of the @Deprecated annotation.

(a) It indicates that the marked element should no longer be used.


(b) It informs the compiler that the element is meant to override an element declared in a
superclass.
(c) It tells the compiler to suppress specific warnings that it would otherwise generate.
(d) It asserts that the code does not perform potentially unsafe operations on its varargs
parameter
(e) It specifies how the marked annotation is stored.

10
auroracs.lk
44) Consider the following program written in Java.

public final class Ex44 {


public static <T> T max(T x, T y) {
return x > y ? x : y;
}
}

When the program is compiled, there were errors generated. Select from among the following, the
valid option(s) which caused the generation of those errors.

(a) final class (b) public static <T> (c) return x > y ? x : y;
(d) max(T x, T y) (e) Type safety

45) Consider the following program written in Java.

public class Ex45 {


public static void main(String args[]){
String str1 = "Hello";
System.out.println(str1.indexOf('e'));
}
}

What would the output of the program be?

(a) 1 (b) 2 (c) 4


(d) 5 (e) error

*******

11
auroracs.lk
Bachelor of Information Technology
Academic Year 2017 – 1st Year Examination – Semester 2
IT2205 – Answers

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


Academic Year 2015/2016 – 1st Year Examination – Semester 2

IT2205 - Programming I
22nd October, 2016
(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 13 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
auroracs.lk
1) Select from among the following, the first name given for Java.

(a) Oak (b) Odd (c) Oslo


(d) J (e) Kawa

2) Select from among the following, valid features which are supported by Java.

(a) Exception Handling (b) Multithreading


(c) Network Programming (d) Operator Overloading
(e) Memory management

3) Select from among the following, what JDK stands for.

(a) Java Design Kit (b) Java Data Kit


(c) Java Definition Kit (d) Java Development Kit
(e) Java Deployment Kit

4) Select from among the following, valid output generated after a Java source file is
successfully compiled.

(a) Object Code (b) Byte Code


(c) P Code (d) Java Compiler
(e) Java Interpreter

5) Java is an Object Oriented Programming language. Select from among the following
correct option(s) which show(s) how Java achieve common information sharing.

(a) Abstraction (b) Pointer manipulation


(c) Exception Handling (d) Inheritance
(e) Polymorphism

6) Select from among the following, valid reference data types available in Java.

(a) System (b) private (c) String


(d) public (e) Scanner

Consider the following program written in Java to answer question 7 – 11.

public class Ex2{


public static void main(String args[]){

float value1=10.0f,value2=20.0f;
int valueOne = 30,valueTwo=40;

//System.out.print(value1 & value2);


System.out.println(valueOne ^ valueTwo);

}
}

2
auroracs.lk
7) Select from among the following, correct option(s) which illustrate/s output of the program when
it is executed.

(a) 54 (b) 55 (c) 56


(d) true (e) error

8) Assume that the following statement is written in the program removing the symbols // in the
program

System.out.print(value1 & value2);

What would the output of the program be?

(a) 0 54 (b) 0 (c) 54


(d) 0.0 54 (e) error

9) Select from among the following, valid symbol(s) available in Java programming language which
has the similar functionalities as the symbol // illustrated in the above program.

(a) /* */ (b) \\ (c) \n


(d) /* **/ (e) /** */

10) Select from among the following, names which are given to each operator used in the program.

(a) bitwise AND  & (b) exclusive OR  ^ (c) comparison  =


(d) logical AND  & (e) arithmetic plus  ^

11) Select from among the following, valid option(s) which shows the same operator(s) belong to
the same category as the operators & and ^.

(a) >> (b) << (c) ~


(d) | (e) <=

Use the following declarations and initializations to evaluate the Java expressions given in
questions 12 - 17. Assume that each expression is evaluated separately in the program.

float x = 10.0f;
int num1=20,num2=30;
double num4 = 100;
boolean value5 = true;
char ch = 'B'; // note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the questions 12 –19.

12) System.out.println("Answer"+value5+num1);

(a) true (b) Answertrue (c) 320


(d) Answertrue20 (e) error

13) System.out.println(value5+ch);

(a) trueB (b) true66 (c) 82


(d) 63 (e) error

3
auroracs.lk
14) System.out.println(x*ch);

(a) 660.0 (b) true (c) 65


(d) 65.0 (e) error

15) System.out.println(num4 + num1 > ch);

(a) 66 (b) true (c) false


(d) 10020 (e) error

16) System.out.println(num4 > num1 > value5);

(a) 12 (b) true (c) 120


(d) false (e) error

17) System.out.println(ch++ + ++ch + ch++);

(a) 198 (b) 200 (c) 201


(d) 202 (e) error

18) System.out.println(num2>num1 && num4/2 >ch);

(a) true (b) 100 (c) false


(d) 30 (e) error

19) System.out.println(x + ch);

(a) true (b) 76.0 (c) 10.066


(d) 76 (e) error

20) Select from among the following, correct operator(s) which use(s) to allocate memory to an array
variable.

(a) malloc (b) alloc (c) new


(d) Object (e) calloc

21) Consider the following program written in Java.

public class Ex5{


public static void main(String args[]){

char ar [] = new char[10];


for (int i = 0; i < 5; ++i) {
ar[i] = 'i';
System.out.print(ar[i] + "");
}
}
}

What would the output of the program be?

(a) 12345 (b) 01234 (c) iiiii


(d) 105106107108109 (e) 4849505152

4
auroracs.lk
22) Consider the following illustration.

* * * *
* * * *
* * * *
* * * *

Select from among the following, the valid program segment, by which the above illustration can
be derived as an output in the command line.

(a) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
System.out.print("* ");
System.out.println();}

(b) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
System.out.println("* ");
System.out.println(); }

(c) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i<=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}

(d) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i>=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

(e) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i==k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

5
auroracs.lk
23) Consider the following illustration.

* * * *
* * *
* *
*

Select from among the following, the valid program segment, by which the above illustration can
be derived as an output in the command line.

(a) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
System.out.print("* ");
System.out.println();}

(b) for(int i=0;i<=3;i++){


for(int k=i;k<=3;k++)
System.out.print("* ");
System.out.println(); }

(c) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i<=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}

(d) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i>=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

(e) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i==k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

6
auroracs.lk
24) Consider the following illustration.

*
*
*
*

Select from among the following, the valid program segment, by which the above illustration can
be derived as an output in the command line.

(a) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
System.out.print("* ");
System.out.println();}

(b) for(int i=0;i<=3;i++){


for(int k=i;k<=3;k++)
System.out.print("* ");
System.out.println(); }

(c) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i<=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}

(d) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i>=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

(e) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i==k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

7
auroracs.lk
25) Consider the following illustration.

*
**
***
****

Select from among the following, the valid program segment, by which the above illustration can
be derived as an output in the command line.

(a) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
System.out.print("* ");
System.out.println();}

(b) for(int i=0;i<=3;i++){


for(int k=i;k<=3;k++)
System.out.print("* ");
System.out.println(); }

(c) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i<=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}

(d) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i>=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

(e) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i==k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

8
auroracs.lk
26) Consider the following illustration.

****
***
**
*

Select from among the following, the valid program segment, by which the above illustration can
be derived as an output in the command line.

(a) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
System.out.print("* ");
System.out.println();}

(b) for(int i=0;i<=3;i++){


for(int k=i;k<=3;k++)
System.out.print("* ");
System.out.println(); }

(c) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i<=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}

(d) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i>=k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

(e) for(int i=0;i<=3;i++){


for(int k=0;k<=3;k++)
if(i==k)
System.out.print("*");
else
System.out.print(" ");
System.out.println(); }

27) Select from among the following, correct statements on access specifies available in Java.

(a) public members of class can be accessed by any code in the program
(b) private members of class can only be accessed by other members of the
class
(c) private members of class can be inherited by a sub class, and become protected
members in sub class
(d) protected members of a class can be inherited by a sub class, and become
private members of the sub class
(e) default members are defined by using the default key word in Java

9
auroracs.lk
Consider the following program written in Java to answer question 28 – 29.

public class Ex7{


public static void main(String args[]){
int ar1[]={1,2,3,4,5,6,7,8};
int ar2[]=new int[4];
int ar3[]=new int[4];
for(int i=0,k=0;i<=7;i++)
if(i<=3)
ar2[i]=ar1[i];
else{
ar3[k]=ar1[i];
k++;
}
for(int i=0;i<=3;i++){
System.out.print(ar2[i]);
System.out.print(ar3[i]);
}
}
}

28) Select from among the following, how many array declarations are written in the program.

(a) 1 (b) 2 (c) 3


(d) 4 (e) 5

29) What would the output of the program be?

(a) 12345678 (b) 87654321 (c) error


(d) 1234 (e) 15263748

Consider following program written in Java to answer question 30 – 35.

class A {
private int i;
public void display() {
System.out.println(i);
}
}
class B extends A {
private int j;
public void display() {
System.out.println(j);
}
}
class DP {
public static void main(String args[]){
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}

10
auroracs.lk
30) Select from among the following, valid description(s) which can be used to name the class
denoted as B in the program.

(a) Super class (b) Child class (c) Derived class


(d) Sub class (e) Base class

31) Select from among the following, valid description(s) which can be used to name the class
denoted as A in the program.

(a) Super class (b) Child class (c) Parent class


(d) Sub class (e) Base class

32) When the program was compiled, there were errors generated. Select from among the following,
the statement(s) which caused to generate those errors.

(a) private int i; (b) private int j; (c) B obj = new B();
(d) class DP (e) B obj = new B();

33) When the program is interpreted, the programmer needs to get an output in the command prompt.
Then the program needs modification. Select from among the following, valid modifications that
can be applied to the program.

(a) int i; (b) int j; (c) introduce getters


(d) introduce setters (e) remove main()

34) Consider the following segment of code which is appearing in the program.
class B extends A

Select from among the following, valid option(s) which shows the object oriented feature which
has been achieved by the given segment of code.

(a) encapsulation (b) information hiding (c) abstraction


(d) inheritance (e) polymorphism

35) In the program one can see the following segment of program in two places.
public void display() { }

Select from among the following, the object oriented feature(s) which has been achieved through
the program segment.

(a) encapsulation (b) overriding (c) overloading


(d) inheritance (e) polymorphism

36) Select from among the following, valid option which shows the super class of all the exceptional
types.

(a) String (b) System (c) throwable


(d) catchable (e) RuntimeException

37) Select from among the following, valid option(s) which can be used to manually throw an
exception.

(a) try (b) finally (c) throw


(d) catch (e) main

11
auroracs.lk
38) Select from among the following, the exception which occur when JVM runs out of memory.

(a) MemoryBoundException (b) OutOfRangeError (c) NullReferenceError


(d) OutOfMemoryError (e) throw

39) Consider the following program written in Java.

public class Ex12 {


public static void main(String args[]){
String name1 = "Manuja Gunasena";
System.out.print(name1.length());
String name2 = "Binusha Mallikarachchi";
System.out.print(name2.charAt(5));
}
}

What would the output of the program be?

(a) 14h (b) 15h (c) 15s


(d) 6s (e) 6h

40) Select from among the following, valid access modifiers allowed in Java interfaces.

(a) static (b) public (c) private


(d) default (e) protected

41) Consider the following program written in Java.

public class Ex13 {


public static void main(String args[]){
String a = "hello i love java";
System.out.println(a.indexOf('o')+" "+a.lastIndexOf('o'));
}
}

What would the output of the program be?

(a) 1 1 (b) 4 4 (c) 5 5


(d) 4 9 (e) 5 10

42) Select from among the following, (the) class/es which used for input and output operations when
work with bytes.

(a) InputStream (b) Reader (c) Read()


(d) FileWriter (e) Writer

43) Select from among the following, valid annotations used in Java.

(a) @Override (b) public (c) private


(d) protected (e) @Deprecated

12
auroracs.lk
44) Select from among the following, valid escape sequences used in Java.

(a) \n (b) // (c) \\


(d) \b (e) /t

45) Consider the following program written in Java.

class Ex45{
public static void main(String args[]){
int x = 2;
int y = 0;
for ( ; y < 10; ++y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}

What would the output of the program be?

(a) 8 (b) 1 3 5 7 9 (c) 0 0 0 0 0 0


(d) 2 4 6 8 10 (e) error

*******

13
auroracs.lk
Bachelor of Information Technology
Academic Year 2016 – 1st Year Examination – Semester 2
IT2205 – Answers
Year 2016

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


Academic Year 2014/2015 – 1st Year Examination – Semester 2

IT2205 - Programming I
25th July, 2015
(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 11 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
auroracs.lk
1) Select from among the following, correct options which can be considered as key words in
Java.

(a) public (b) static (c) void


(d) main (e) String

2) An interesting feature of Java is its bytecode. Sometimes it is called a computer within a


computer. Select from among the following, correct feature(s) which go(es) well with the
byte code in Java.

(a) Exception Handling (b) Multithreading


(c) Network Programming (d) Publish data in the Internet
(e) Platform Independence

3) Select from among the following, correct statement(s) on Java programming language.

Java is:
(a) case sensitive. (b) strongly typed.
(c) supportive in Internet programming. (d) simple in syntaxes.
(e) a fully object oriented language.

4) Consider the following expression in Java written as a program.

var1=8;

When the program was compiled, there were errors generated. Select from among the following
correct statement(s), which will describe the underlying concept behind the erroneous situation.

(a) Secure programming (b) Case sensitiveness


(c) Strongly typed nature (d) Socket programming.
(e) Auto-boxing

5) Consider the following expression written in a Java program.

final float number = 78.5f;


number = 45.4f;

Select from among the following, incorrect statement(s) regarding the program.

(a) It will generate compile time errors. (b) final is a key word to define constants.
(c) At run time number variable will (d) number is a key word in Java
hold 45.4f.
(e) At run time the program will output
45.4.

6) Select from among the following, data types which can be considered as reference data types.

(a) String (b) int (c) System


(d) Scanner (e) void

2
auroracs.lk
7) Select from among the following, correct option(s) which illustrate/s String literals in Java.

(a) 7 (b) “Sri Lanka” (c) “N”


(d) ‘C’ (e) “76”

8) Consider the following program written in Java.

public class Ex8{


public static void main(String args[]){
int val;
String str;
boolean flag;
System.out.println(val+" "+str+" "+flag);
}
}

What would the output of the program be?

(a) val+" "+str+" "+flag (b) 0 null false (c) 0 null true
(d) 0 0 0 (e) error

9) Select from among the following, valid variable name(s) which is/are used in Java.

(a) number1 (b) &Mark (c) _value


(d) %Mark (e) 2Number

10) Consider the following program written in Java.

public class Ex10{


public static void main(String args[]){
System.out.println(args[0]+args[1]);
}
}

After compiling successfully, the program was executed by issuing the following command.

java Ex10 4 1

What would the output of the program be?

(a) 5 (b) Ex104 (c) 41


(d) javaEx10 (e) error

11) Consider the following number.

222222222222L

Select from among the following, option(s) which cannot be used to hold the above value as it is.

(a) int (b) long (c) short


(d) byte (e) double

3
auroracs.lk
Use the following declarations and initializations to evaluate the Java expressions given in
questions 12 - 17. Assume that each expression is evaluated separately in the program.

int value1 = 2;
short num1 = 10;
double num2 = 100;
char ch = 'A'; // note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the questions 12 – 17.

12) System.out.println(value1*(num1+num2));

(a) 2.0 (b) true (c) 220.0


(d) 200 (e) error

13) System.out.println(ch>value1);

(a) 2.0 (b) true (c) 220.0


(d) 63 (e) error

14) System.out.println(ch < ++ch);

(a) 66 (b) true (c) 65


(d) 65.0 (e) error

15) System.out.println((int)(ch=ch++));

(a) 66 (b) B (c) 65


(d) A (e) error

16) System.out.println(num1 & value1);

(a) 12 (b) true (c) 65


(d) 2 (e) error

17) System.out.println(num1 & value1 > num2);

(a) 12 (b) true (c) 65


(d) 2 (e) error

18) Consider the following program written in Java.

public class Ex18{


public static void main(String args[]){
for(int i=0;i<5;i++){
if(i==2) break;
System.out.print(i); }}}

What would the output of the program be?

(a) 012345 (b) 01234 (c) 0123


(d) 012 (e) 01

4
auroracs.lk
19) Consider the following program written in Java.

public class Ex19{


public static void main(String args[]){
int ar[]={51,12,37,4,15,61};
int value=ar[0];
for(int k=1;k<=5;k++){
if(value>ar[k])
value=ar[k];
}
System.out.println(value);
}
}

What would the output of the program be?

(a) 51,12,37,4,15,61 (b) 61 (c) 4


(d) 51123741561 (e) 12

20) Consider the following program written in Java.

public class Ex20{


public static void main(String args[]){
char ar[]={'A','B','C','D','E','F'};//ASCII value of A is 65
int value=0;
for(int k=0;k<=ar.length-1;k++){
value=ar[k];
if(value%2==1)
System.out.print(value);
}
}
}

What would the output of the program be?

(a) 666870 (b) 656769 (c) ACE


(d) BDF (e) error

21) Select from among the following, relevant key words, which describe the object orientation feature
called data hiding.

(a) extends (b) public (c) private


(d) protected (e) access

22) Select from among the following, valid option(s), which can be considered as (a) class (es),
considering a school system.

(a) Manuja Gunasena (b) Teacher (c) Subject


(d) Grade 6A (e) Student

23) Select from among the following, valid option(s), which can be considered as (an) object (s),

5
auroracs.lk
considering a school system.

(a) Manuja Gunasena (b) Teacher (c) Subject


(d) Grade 6A (e) Student

Consider the following class declaration written in Java to answer questions 24 – 29.

public class Ex21{


public int count1;
private int count2;
private static int count3;
public Ex21(){}
public setCount1(){
count1=count1;
}
public getCount1(){
return count1;
}
public setCount2(){
count2=count2;
}
public getCount2(){
return count2;
}
}

24) Select from among the following, valid instance variable/s written in the program.

(a) count1 (b) count2 (c) count3


(d) Ex21() (e) Ex21

25) When the class was compiled, there were errors generated. The programmer noticed that get
methods were not written correctly. Select from among the following, valid get method signatures
to be introduced in the class declaration.

(a) public Ex21()


(b) public getCount1(int count1)
(c) public int getCount2()
(d) public getCount2(int count2)
(e) public int getCount1()

26) When the class was compiled, there were errors generated. The programmer noticed that set
methods were not written correctly. Select from among the following, valid set method signatures
to be introduced in the class declaration.

(a) public int count1


(b) public int setCount1(int a)
(c) public void setCount2(int count2)
(d) public void setCount1(int count1)
(e) public int setCount2(int b)

27) The programmer has identified another two programming statements causing confusion to read.

6
auroracs.lk
They are illustrated below.

count1=count1;
count2=count2;

Select from among the following, valid option(s) that can be used to modify the program to
increase the readability of the program.

(a) count1=count2 (b) this.count2=count2 (c) super.count1=count1


(d) this.count1=count1 (e) count1==count2

28) After correcting all the errors and confusions which existed in the Ex21 class, one has written the
following program and compiled.

class DP{
public static void main(String args[]){
Ex21 ob= new Ex21;
count1=21;
System.out.println(count1);
}
}

When the program was Compiled, errors were generated. Select from among the following, correct
option(s) to substitute the programming statement(s) to avoid errors.

Existing Code  Proposed Code


(a) Ex21 ob= new Ex21;  Ex21 ob= new Ex21();
(b) count1=21;  21= ob.count1;
(c) Ex21 ob= new Ex21;  Ex21 ob()= new Ex21;
(d) count1=21;  ob.count1=21;
(e) System.out.println(count1);  System.out.println(ob.count1);

29) Consider the following program written in Java with new programming statements.
class DP{
public static void main(String args[]){
Ex21 ob= new Ex21();
ob.count2=80;
System.out.println(ob.count2);
}
}

When the program was compiled, errors were generated. Select from among the following correct
option(s) to avoid errors generated.

Existing Code  Proposed Code


(a) Ex21 ob= new Ex21();  Ex21 ob= new Ex21(80);
(b) ob.count2=80;  ob.setCount2(80);
(c) Ex21 ob= new Ex21();  Ex21 ob= new Ex21()=80;
(d) ob.count2=80;  ob.setCount2=80;
(e) System.out.println(ob.count2);  System.out.println(ob.getCount2());

30) Consider the following program written in Java.

7
auroracs.lk
class What{
public void work(int value){
if(value>0){
System.out.print(value+"");
work(value - 1);
}
}
}
class Ex30{
public static void main(String args[]){
What ob= new What();
ob.work(3);
}
}

What would the output of the program be?

(a) 6 (b) 321 (c) 7


(d) 0 (e) error

31) One has created the package having the name MyPack in the following path in windows
environment.
C:\MyPrograms\Java\MyPack

Select from among the following, valid option(s) to be included in the package path in the
CLASSPATH environment variable.

(a) C:\MyPrograms\Java\MyPack (b) C:\MyPrograms (c) C:\MyPrograms\Java


(d) C:/MyPrograms/Java/MyPack (e) C:/MyPrograms

32) Select from among the following, correct option(s) to specify the class path in the command prompt
while interacting with a Java program.

(a) –processorpath (b) –setclasspath (c) –argumentpath


(d) –classpath (e) –bytecodepath

33) Consider the following program written in Java.

public class Ex32{


public static void main(String args[]){
int ar[]=new int[ -3 ];//note that negative three is assigned here
ar[-0]=1;
ar[-1]=7;
ar[-2]=9;
System.out.println(ar[1]);
}
}

When the program is executed an exception is generated. Select from among the following, the
exception that has been generated during the execution of the above program.

(a) ArrayIndexOutOfBound (b) Arithmetic (c) IllegalArgument


(d) NegativeArraySize (e) Security

Consider the following programs written in Java to answer questions 34 – 35.

8
auroracs.lk
interface MyInterface{
public int value=9;
void walk();
void eat();
}

class MyClass implements MyInterface{


void walk(){
System.out.println("walking");
}
}

34) Select from among the following, valid statement(s) which can be used with the above programs.

(a) class MyClass extends class A // A is a name of a class


(b) class MyClass extends class X, Y //X and Y are names of classes
(c) class MyInterface extends MyClass
(d) interface OurInterface extends MyInterface
(e) interface NewInterface implements OurInterface, MyInterfar

35) When the above programs were compiled, compile time errors were generated. Select from among
the following valid option(s) which can cause the generation of those errors.

(a) System.out.println("walking"); (b) void eat(); (c) void walk();


(d) interface MyInterface (e) public int value=9;

36) Select among the following, valid option(s) that can be considered unchecked exceptions defined in
the java.lang package.

(a) ArrayIndexOutOfBound (b) Arithmetic (c) IllegalArgument


(d) ClassNotFound (e) Security

37) Consider the following program written in Java.

enum Kingdoms {
Kotte, Polonnaruwa, Seethawaka, Kandy, Anuradhapura
}
class Ex34 {
public static void main(String args[])
{
Kingdoms ap;

ap = Kingdoms.valueOf("Kandy");
System.out.println(ap);

}
}

What would the output of the program be?

(a) 3 (b) 4 (c) 5


(d) Kandy (e) error

9
auroracs.lk
38) Consider the following programming statements written in Java.

Integer obj= new Interger(8);


Obj = 100;

Select from among the following, (a) valid option(s) which describe the Obj = 100; statement
methodically.

(a) boxing (b) autoboxing (c) unboxing


(d) autounboxing (e) enumeration

39) Select from among the following the package in which the Annotation interface is declared.

(a) java.lang (b) java.net (c) java.applet


(d) java.util (e) java.awt

40) One can write generalized classes, interfaces or methods referring to the Object class in the Java
API. But that practice lead to programming problems and nowadays programmers use generics to
avoid such problems in a standard manner. Select from among the following, the programming
problem caused when the Object class is used and averted due to the proper usage of generics in
Java.

(a) Encapsulation (b) Casting (c) Modular programming


(d) Platform independency (e) Type safety

41) Consider the following program written in Java.

public class Ex41{


public static void main(String args[]){
String str = "University of Colombo";
System.out.println(indexOf(‘s’));

}
}

What would have been the output of the program when it was executed at the time of paper setting?

(a) 8 (b) 6 (c) 7


(d) 9 (e) error

42) Consider the following program written in Java.

public class Ex41{


public static void main(String args[]){
String str = "Galleface";
System.out.println(str.endsWith("Face"));
}
}

What would the output of the program be?

(a) Galleface (b) true (c) false


(d) GalleFace (e) error

10
auroracs.lk
43) Select from among the following, valid interfaces defined in the Collection Framework of Java.

(a) Collection (b) SortedSet (c) NavigatableSet


(d) Set (e) Queue

44) Select from among the following, valid method(s), which yield a boolean output related to the
Scanner class.

(a) hasNext() (b) hasNextBoolean() (c) hasNextByte()


(d) nextInt() (e) nextValue()

45) Consider the following program written in Java.

import java.io.*;
class Ex45 {
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
do {
c = (char) br.read();
System.out.print(c);
} while(c != 'q');
}
}

When the program was executed the following message was displayed in the command prompt.

Enter characters, 'q' to quit.

Then the following characters were entered in the command prompt.

ABCq

What would the output of the program be?

(a) ABCq (b) ABC (c) abcq


(d) 656667 (e) error

*******

11
auroracs.lk
Bachelor of Information Technology
Academic Year 2014/2015 – 1st Year Examination – Semester 2
IT2205 – Answers
Year 2015

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

*Answer for Q11 is (a, c, d, e)

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


st
Academic Year 2013/2014 – 1 Year Examination – Semester 2

IT2205 - Programming I
26th July, 2014
(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
auroracs.lk
1) Consider the following program written in Java.

public class Ex1{


public static void main(String args[]){
System.out.println("ChamaraMadushanka");
}
}

Select from among the following, key words that can be seen in the program.

(a) class (b) main (c) Ex1


(d) Out (e) Chamara

Consider the following program written in Java to answer question 2 – 6.

public class Ex2{


public static void main(String args[]){
System.out.println("University of \n Colombo");
}
}

2) Select from among the following, (a) possible identifier(s) that can be used in saving the
above Java file which is written in a notepad in Windows environment.

(a) Ex2 (b) class (c) void


(d) University (e) \n

3) Select from among the following, the full name of the bytecode file which could be
generated, after compiling the above program successfully.

(a) class (b) Ex2.class (c) Ex2


(d) Ex2.java (e) String.class

4) Select from among the following the correct option/s which show/s the output of the given
Java program.

(a) ("University of \n Colombo"); (b) "University of \n Colombo"


(c) University of Colombo (d) University of
\n Colombo
(e) University of
Colombo

5) In the given program there is a notation \n written there.

Select from among the following, the name which can be given to that category of
notations.

(a) Unary operators (b) Selection structures (c) Escape sequences


(d) Bitwise operators (e) String literals

2
auroracs.lk
6) Select from among the following, similar and (a) valid notation/s like the notation \n illustrated in
the above program.

(a) \\ (b) // (c) \t


(d) \” (e) \j

Consider the following program written in Java to answer questions 7 – 10.

public class Ex3{


public static void main(String args[]){
int num1 = 7;
//int num1 = 10;
//System.out.println("value of num1"+num1);
System.out.println(num1);
}
}

7) What would the output of the program be?

(a) value of num1"+num1 (b) num1 (c) 7


(d) 10 (e) error

8) What would the output of the program be, if the // notation which is marked in the program is
removed?

(a) value of num1"+num1 (b) num1 (c) 7


(d) 10 (e) error

9) Select from among the following, similar and (a) valid notation/s like // which is/are used in Java.

(a) /* */ (b) /** */ (c) ‘


(d) \* *\ (e) \\

10) One has replaced the following statement without changing the other statements shown in the
program.

System.out.println("value of num1"+num1);

The new statement is illustrated below.

System.out.print("value of num1"+num1);

What would the output of the program be?

(a) value of num1 (b) num (c) 7


num1 1
(d) 10 (e) error

11) Select from among the following, programming languages which has/ve a close relationship with
the development phase of the Java programming language.

(a) C (b) Android (c) Visual Basic


(d) Python (e) C++

3
auroracs.lk
12) Consider the following program written in Java.

public class Ex4{


public static void main(String args[]){
int value = 123_456_789;

System.out.println(value);
}
}

What would the output of the program be?

(a) 123_456_789 (b) 123456789 (c) 987654321


(d) 45 (e) error

13) Consider the following program written in Java.

public class Ex5{


public static void main(String args[]){
int value = 0B1010;

System.out.println(value);
}
}

What would the output of the program be?

(a) 0B1010 (b) 10 (c) 2


(d) 0101B0 (e) error

14) Consider the following program written in Java.

public class Ex7{


public static void main(String args[]){
int value = 0X7A;

System.out.println(value);
}
}

What would the output of the program be?

(a) 0X7A (b) 122 (c) A7X0


(d) 7 (e) 11

Consider the following program written in Java to answer questions 15 – 16.

public class Ex8{


public static void main(String args[]){
int value = 8;
float num1 = 10.0f;
int add= value + num1;
System.out.println(add);
}}

4
auroracs.lk
15) What would the output of the program be?

(a) 8 (b) 10 (c) 18.0


(d) 18.0f (e) error

16) Consider the following programming statements written in a table where the column A is marked
with serial numbers. The column B shows the existing codes and in C new programming statements
are introduced to replace with the existing ones.

Column A Column B Column C


1 System.out.println(add); System.out.println(value);
2 System.out.println(add); System.out.println(add +value);
3 int add= value + num1; int add= (int)(value + num1);
4 int add= value + num1; int add= (int)value + num1;
5 int add= value + num1; int (int)add= value + num1;

Select from among the following, the correct option/s to see the output of the program as 18.

(a) only 1 (b) only 2 (c) only 3


(d) only 4 (e) only 5

Use the following declarations and initializations to evaluate the Java expressions given in
questions 17 - 21. Assume that each expression is evaluated separately in the program.
int value1 = 1;
float num1 = 5.0f;
byte num2 = 10;
char ch = 'A'; // note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the questions 17 – 21.

17) System.out.println(ch + num2);

(a) 10 (b) A (c) 6510


(d) 75 (e) error

18) System.out.println(num1 + num2 + value1);

(a) 16.0 (b) 17.0 (c) 18.0


(d) 19.0 (e) error

19) System.out.println(value1 = ch);

(a) A (b) 65 (c) 66


(d) B (e) error

20) System.out.println(ch = num1 * 13.0f);

(a) A (b) 65 (c) true


(d) A65 (e) error

21) System.out.println(ch = 68);

(a) 68 (b) D (c) true


(d) false (e) error

5
auroracs.lk
Consider the following class declaration written in Java to answer questions 22 – 28.

class X{
private int v1;
private int v2;
public X(){
System.out.println("Class X");
v1=1;
v2=2;
}
}

class Y extends X{
private int v3;
private static int v4;
public Y(){
System.out.println("Class Y");
v3=3;
v4=4;
}
}

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

(a) v1 (b) v2 (c) v3


(d) v4 (e) X()

23) Select among the following, (a) valid option/s that can be considered as class variables.

(a) v1 (b) v2 (c) v3


(d) v4 (e) X()

24) Select from among the following, (a) valid object orientation feature/s which is/are related with the
word extends.

(a) Abstraction (b) Information Hiding (c) Encapsulation


(d) Inheritance (e) Polymorphism

25) Consider the following program written in Java with the main method in it and saved in the same
folder where the above class declarations are saved.

public class Ex11{


public static void main(String args[]){
X obj1= new X();

}
}

What would the output of the program be when the program is executed?

(a) Class Y (b) Class X (c) 2


(d) 3 (e) error

6
auroracs.lk
26) Consider the following program with the main method noting the modifications to the program of
question no 25.

public class Ex11{


public static void main(String args[]){
Y obj1= new Y();
}
}

What would the output of the program be when the program is executed?

(a) Class X (b) Class X


Class Y
(c) Class Y (d) Class Y
Class Y Class X
(e) Class Y

27) Consider the following program written in Java with the main method and new modifications.

public class Ex11{


public static void main(String args[]){
X obj1= new X();

System.out.println(obj1.v1);
}
}

What would the output of the program be?

(a) Class A (b) Class B (c) 1


(d) 2 (e) error

28) Consider the following program written in Java with the main method and new modifications.

public class Ex11{


public static void main(String args[]){
X obj1= new X();

System.out.println(obj1.v4);
}
}

What would the output of the program be?

(a) Class A (b) Class B (c) 4


(d) 3 (e) error

29) In Java, elements of an array are automatically initialized to some default value.
What is the default value for the elements of an array of integers?

(a) 0 (b) “0” (c) null


(d) 1 (e) ‘1’

7
auroracs.lk
30) Consider the following program written in Java.

public class Ex12{


public static void main(String args[]){
int a = 6;
int b = 12;
while(a<b){
System.out.println("In the loop");
a+=2;
b-=2;
}
}
}

Select from among the following, the number of times the phrase In the loop is printed in the
command prompt, when the program is executed.

(a) 1 (b) 2 (c) 3


(d) 4 (e) 5

31) Consider the following program written in Java.

public class Ex13{


public static void main(String args[]){
int[] x = {5,6,7,8,9};
int[] y = x;
y[2] = 10;
System.out.println(x[2]);
}
}

What would the output of the program be?

(a) 5 (b) 7 (c) 9


(d) 10 (e) error

32) Consider the following program written in Java.

public class Ex14{


public static void main(String args[]){
int[] x = {5,6,7,8,9};
for(int i=0;i<x.length-1;i++){
if(i==3) continue;
System.out.print(x[i]);
}
}
}

What would the output of the program be?

(a) 56789 (b) 5678 (c) 567


(d) 56 (e) 5

8
auroracs.lk
33) Consider the following program written in Java.

class Access{
static int x;
void increment(){
x++;
}
}

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

{
Access obj1 = new Access();
Access obj2 = new Access();
obj1.x = 0;
obj1.increment();
obj2.increment();

System.out.println(obj1.x + " " + obj2.x);

}
}

What would the output of the program be?

(a) 1 1 (b) 2 2 (c) 3 3


(d) 1 2 (e) 0 1

34) Consider the following program written in Java.

class Ex16 {
public static void main (String args[]) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}

After compiling the program successfully, the program was executed by issuing the following
command and related arguments.

java Ex16 1234 12 1

What would the output of the program be?

(a) 1 (b) 1234 (c) 1234121


(d) 12341234 (e) error

35) Select from among the following, (a) valid method/s available in an Applet.

(a) init() (b) start() (c) stop()


(d) main() (e) destroy()

9
auroracs.lk
36) Consider the following program written in Java.

import java.awt.*;
import java.applet.*;
public class myApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}

What is the message which will be displayed from the program?

(a) A Simple Applet, 20, 20 (b) "A Simple Applet"


(c) Graphics g (d) A Simple Applet
(e) 2020202020202020202020202020202020

37) Consider the following program written in Java.

class Ex17 {
public static void main(String args[]) {
try {
System.out.print("Hello" + " " + 1 / 0);
}
finally {
System.out.print("World");
}
}
}

What would the output of the program be?

(a) Hello
(b) World
(c) WorldException in thread "main" java.lang.ArithmeticException: / by zero
(d) Exception in thread "main" java.lang.ArithmeticException: / by zero
(e) Hello 1/0

38) Select from among the following, (a) valid key word/s that can be used to manually throw an
exception.

(a) try (b) finally (c) throw


(d) catch (e) new

39) Consider the following program written in Java.

public class Ex18{


public static void main(String args[]){
char array[]={'F','G','H'};
for(char i : array)
System.out.print(i);
}}

What would the output of the program be?

(a) FGH (b) hgf (c) 707172


(d) 102103104 (e) error

10
auroracs.lk
40) Consider the following program written in Java.

class Pair<T>{
private T first;
private T second;

public Pair() { first = null; second = null; }


public Pair(T firstItem, T secondItem) {
first = firstItem;
second = secondItem;
}
public void show() {
System.out.println(first+" "+second);
}
}
class Ex19{
public static void main(String args[]){
Pair<String> pair1 = new Pair<String>("Happy", "Day");
pair1.show();
Pair<int> pair1 = new Pair<int>(1, 2);
pair1.show();
}
}

Select from among the following, valid statements on the program.

(a) The program can be compiled but cannot be executed.


(b) The program cannot be compiled successfully.
(c) class Pair<T> is incorrect.
(d) Pair<String> pair1 = new Pair<String>("Happy", "Day"); is incorrect.
(e) Pair<int> pair1 = new Pair<int>("Happy", "Day"); is incorrect.

41) Select from among the following, (a) valid operation/s/ to concatenate two more string objects.

(a) + (b) ++ (c) &


(d) conCat (e) ||

42) Consider the following program written in Java.

class Ex21 {
public static void main(String args[]) {
char chars[] = {'f', 'g', 'h'};
String s = new String(chars);
System.out.println(s);
}
}

What would the output of the program be?

(a) fgh (b) f (c) 102103104


(d) 102 (e) error

11
auroracs.lk
43) Consider the following program written in Java.

class Ex22 {
public static void main(String args[])
{
String obj = "UNIVERSITY";

System.out.println(obj.charAt(3));

}
}

What would the output of the program be?

(a) 3 (b) I (c) V


(d) UNI (e) error

44) Consider the following phrase on Java noting the blank.

“In Java …................. package is used by compiler itself. So it does not need to be imported for
use”

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

(a) applet (b) io (c) lang


(d) net (e) util

45) Select from among the following, the key word which can be used by a class to use an interface
which has been defined previously.

(a) extends (b) new (c) generic


(d) implements (e) inherit

*******

12
auroracs.lk
Bachelor of Information Technology
Academic Year 2013/2014 – 1st Year Examination – Semester 2
IT2205– Answers
Year 2014

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


st
Academic Year 2012/2013 – 1 Year Examination – Semester 2

IT2204 - Programming I
27th July, 2013
(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
auroracs.lk
1) Consider the following program written in Java.

public class First{


public static void main(String args[]){
System.out.println("Chamara Madushanka");
}
}

Select from among the following, (a) suitable editor(s) that can be used to write the
program in the windows environments.

(a) notepad (b) vi (c) Microsoft word


(d) Word pad (e) Paint in windows

2) Select from among the following, (a) possible identifier(s) that can be used in naming
variable in Java.

(a) Teacher (b) Subject (c) Student


(d) firstName (e) 2ndName

3) Select from among the following, valid escape sequences used in Java.

(a) \n (b) \t (c) \\


(d) \” (e) \’

4) Consider the following two lines.

Sigiriya
Nuwaraeliya

Select from among the following, (a) valid Java statement(s) which can be used to get those
lines printed.

(a) System.out.println("Sigiriya Nuwaraeliya ");


(b) System.out.println("Sigiriya Nuwaraeliya "\n);.
(c) System.out.println("Sigiriya \nNuwaraeliya ");
(d) System.out.print("Sigiriya");
System.out.println("Nuwaraeliya ");
(e) System.out.println("Sigiriya");System.out.println("Nuwaraeliya ");

5) One has saved a Java source file giving the following file name.

Student.java

Select from among the following, the class name which has been used in the file.

(a) Student (b) main (c) Java


(d) String (e) int

2
auroracs.lk
6) One has saved a Java source file giving the following name.

Customer.java

Select from among the following, the correct option(s) that can used to create a documentation of
the Customer class as web page.

(a) javadoc (b) java (c) javac


(d) .txt (e) Customer.class

7) Select from among the following, the valid option(s) which can be considered as comments in Java.

(a) // (b) /** */ (c) /* */


(d) ‘ (e) “

8) Select from among the following, valid relational operators.

(a) == (b) >= (c) =<


(d) && (e) ++

9) Select from among the following, (a) incorrect statement(s) related to number literals.

(a) int num1 = ‘A’; (b) float num 2= 4.5; (c) double num3= 100.8D;
(d) long num4 = 100L; (e) short num5 = 100S;

Consider the following program written in Java to answer questions 10 and 11.

public class Ex10{


public static void main(String args[]){
System.out.println("Anuradhapura");
}
}

10) Select from among the following, the valid option(s) that can be considered to prove that a Java is
an Object Oriented programming language.

(a) args[] (b) Anuradhapura (c) class


(d) main (e) { }

11) Select from among the following, what can be considered as keywords in Java.

(a) args[] (b) void (c) class


(d) main (e) String

12) Select from among the following, the company where the Java language was initially developed.

(a) Oracle Company (b) Sun Microsystems (c) Microsoft Company


(d) Oak Company (e) Java Company

3
auroracs.lk
Use the following declarations and initializations to evaluate the Java expressions given in
questions 13 - 17. Assume that each expression is evaluated separately in the program.
int num1 = 10,num2 = 15,num3 = 20;
char ch = 'A'; // note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the questions 13 – 17.

13) System.out.println(num1 < num2 :”310”?”500”);

(a) 310 (b) 300 (c) 500


(d) 610 (e) error

14) System.out.println(num1 + (num2 * num3));

(a) 310 (b) 300 (c) 500


(d) 610 (e) error

15) System.out.println("values are"+ ch + num1);

(a) values areA (b) values areA10 (c) values are10


(d) values are75 (e) values are

16) System.out.println( ch );

(a) A (b) 65 (c) true


(d) A65 (e) error

17) System.out.println( ch > num1 );

(a) 65 (b) A (c) true


(d) 75 (e) error

Consider the following pool of Java statements to answer questions 18 – 23. 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. Answers for each option of questions 18 to 23 is given as a list of identifier numbers
indicating the program statements.

Identifier Java statements/Curly Brackets


1 {
2 }
3 char area = base* (height/2);
4 boolean area = base* (height/2);
5 int area = base / (height/2);
6 for(int 0 = i; i<= 3 ; i++)
7 for(int i = 0; I = 3 ; i++)
8 for(int i = 0; i<= i ; i++)
9 System.out.println(x);
10 if(i* 2==1)
11 System.out.println(“i”);
12 double final poundsToKg = 0.45359237;
13 System.out.println(y);
14 Switch(i)
15 double kilograms = pounds % 0.45359237;

4
auroracs.lk
16 double kilograms = pounds = 0.45359237;
17 for(int i = 0; i<= 3 ; i++)
18 for(int k = i ; k<= 3 ; k++)
19 for(int k = 0 ; k<= 3 ; k++)
20 System.out.print("* ");
21 System.out.println();
22 else if(average != 65.0)
23 if(average != 75.0)
24 double average = 67.0;
25 if(average >= 75.0)
26 System.out.println("B");
27 else if(average >= 65.0)
28 System.out.println("A");
29 else if(average >= 45.0)
30 System.out.println("C");
31 if(average => 75.0)
32 Else
33 else if(average => 65.0)
34 System.out.println("F");
35 else if(average => 45.0)
36 int area = base* (height/2);
37 System.out.println(area);
38 System.out.println("Area of Triangle is" + area);
39 int area = (base* height)/2;
40 int height = 12;
41 int base = 4;
42 System.out.println(i);
43 if(i%2==1)
44 if(i%2==0)
45 for(int i = 50; i <= 100 ; i++)
46 double kilograms = pounds * poundsToKg;
47 final double poundsToKg = 0.45359237;
48 System.out.println( "You entered: " + pounds + " pounds,
which is equivalent to " +kilograms+ " kilograms.");
49 double kilograms = pounds * 0.45359237;
50 double pounds = 100;

18) It is required to write a Java program to convert 100 pounds to kilograms considering that one
pound is equal to 0.45359237 kilogram.

The blank space in the following Java program is to be filled with the identifiers from the table.

public class PoundsToKilo{


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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 50,49,48 (b) 47,49,16,48,12


(c) 12,50,46,48 (d) 47,50,46,48
(e) 15,46,47

5
auroracs.lk
19) Write a Java program to print all the odd numbers in the number range 50 to 100 horizontally in the
command prompt. One has to use the for control structure in writing the program and the name of
the control variable is i.

The blank space in the following Java program is to be filled with the identifiers from the table.

public class OddNumbers{


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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 45,44,42,10 (b) 45,43,42


(c) 45,1,43,42,2 (d) 45,1,44,42,2,10
(e) 45,2,44,42,1

20) It is required to write a Java program to calculate an area of a triangle. Area of a triangle is
calculated using the following expression.
Area = ½ X b X h
Here b is the base of the triangle and h is the height of the triangle. Assume that the value of b is 4
and the value of h is 12.

The blank space in the following Java program is to be filled with the identifiers from the table.

public class AreaOfTriangle {


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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 37,4,39,5,41,14 (b) 41,40,39,37


(c) 41,40,39,37,38 (d) 41,40,39,37,38
(e) 36,3,41,37,14

21) It is required to write a Java program to evaluate the grade of a candidate using if statement. When
the average of the candidate is given according to the following table the grade has to be evaluated.

If the average is greater than or equal to 75 the grade is A,


If the average is 74 - 65 the grade is B,
If the average is 64 - 45 the grade is C,
If the average is less than or equal to 44 the grade is evaluated as F.

The blank space in the following Java program is to be filled with the identifiers from the table.

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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 24,25,26,23,28,29,30,32,34 (b) 24,31,28,27,33,29,23,35,34


(c) 24,25,28,27,26,29,30,32,34 (d) 24,25,22,27,29,30,32,34,14
(e) 24,22,28,27,26,29,30,43,44

6
auroracs.lk
22) Consider the following style below.

* * * *
* * * *
* * * *
* * * *

It is required to write a Java program to illustrate the given style using for control structures.
The blank space in the following Java program is to be filled with the identifiers in the table.

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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 1,17,19,20,21,9,2 (b) 19,1,17,20,21,2


(c) 17,19,20,21,14,2 (d) 17,1,19,20,21,2
(e) 17,11,1,18,20,21,2,13

23) Consider the following style below.

* * * *
* * *
* *
*

It is required to write a Java program to illustrate the given style using for control structure.
The blank space in the following Java program is to be filled with the identifiers in the table.

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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 1,17,19,6,21,2 (b) 19,1,7,20,21,2


(c) 17,19,7,21,14 (d) 17,1,19,8,21,2
(e) 17,1,18,20,21,2

Consider the following program written in Java to answer questions 24 and 25.

class What
{
public static void main(String args[])
{
String s="malayalam";
int i;
int n=s.length();
String str="";
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);

System.out.println(str);
}
}

7
auroracs.lk
24) What would the output of the program be?

(a) malayalam (b) mala (c) error


(d) yalam (e) 123456789

25) Assume that one has changed the String s="malayalam"; statement as follows.

String s="Bangkok or Nuwaraeliya";

What would the output of the program be?

(a) ayilearawuN ro kokgnaB (b) Bangkok or Nuwaraeliya


(c) Bangkok (d) kokgnaB
(e) 123456789101112131415161718192021

26) Select from among the following, (a) valid option(s) that can be considered as packages in Java.

(a) lang (b) applet (c) io


(d) net (e) awt

27) Consider the following statement noting the word blank.

“Environment variables can be set by right clicking blank icon and then selecting the properties
menu in the Windows Operating System”

Select from among the following the suitable option to substitute for the word blank in the above
statement.

(a) Internet Explorer (b) My Computer (c) Recycle Bin


(d) Program Files (e) Notepad

28) Consider the following program written in Java.

public class Ex28{


public static void main(String args[]){

int number = 1234;

int num2 =0;

while(number > 0){


num2 = number % 10;
number = number / 10;
System.out.print(num2);
}
}
}

What would the output of the program be?

(a) 1234 (b) 4321 (c) 123


(d) 321 (e) error

8
auroracs.lk
29) Consider the following program written in Java.

public class Ex29{


public static void main(String args[]){
int number = 5, i = 0;

int num2[] = new int[8];

while(number > 0){


num2[i] = number % 2;
number = number / 2;
i++;
}
for(int k=i-1; k>=0 ; k--)
System.out.print(num2[k]) ;
}
}

What would the output of the program be?

(a) 5 (b) 100 (c) 101


(d) 55555555 (e) error

30) Which of the following is the smallest Integer data type?

(a) double (b) smallest (c) short


(d) int (e) byte

31) Consider the following program written in Java.

public class Ex30{


public static void main(String args[]){
char ch = 'A'; // note that the ASCII value of A is 65
int i = 0;
do{
System.out.print(ch);
ch++;
i++;
}while (i<=3);
}
}

What would the output of the program be?

(a) A (b) 1234 (c) ABCD


(d) 65666768 (e) error

9
auroracs.lk
32) Consider the following program written in Java.

public class Ex30{


public static void main(String args[]){
char ch = 'A'; // note that the ASCII value of A is 65
int i = 0;
int values[] = new int[8];
do{
values[i] = ch;
System.out.print(values[i]);
ch++;
i++;
}while (i<=3);
}
}

What would the output of the program be?

(a) A (b) 1234 (c) ABCD


(d) 65666768 (e) error

33) Consider the following program written in Java.

public class Ex31{


public static void main(String args[]){
int array[]={2,4,6,8};

for(int i : array[i])
System.out.print(i);
}
}

What would the output of the program be?

(a) 2468 (b) 8462 (c) 24


(d) 1234 (e) error

Consider the following program written in Java to answer questions 34 - 41.

class Student{
private String name;
private int age;
public static String city;

Student(){ }

Student(int a){age = a;}


public void setName(String n){ name = n;}
public String getName(){return name;}

private int getAge(){return age;}

public void setCity(String c){city = c;}


public String getCity(){ return city;}
}

10
auroracs.lk
34) When considering the features of object orientation, which feature(s) is/are shown clearly in the
program?

(a) Abstraction (b) Encapsulation (c) Data hiding


(d) Inheritance (e) Polymorphism

35) Assume that the following program is also going to be written in the same notepad where the
Student class also has been written.

public class DriverProgram{


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

Select from among the following, a suitable file name to save the notepad.

(a) Student (b) DriverProgram (c) main


(d) MySchool (e) String

36) Select from among the following, (a) variable(s) that can be considered as (an) instance variable(s).

(a) name (b) getName (c) age


(d) city (e) setName

37) Select from among the following, (a) variable(s) that can be considered as (a) class variable(s).

(a) name (b) getName (c) age


(d) city (e) setName

38) Select from among the following, (a) method(s) which can be considered as (a) constructor(s).

(a) Student(){ } (b) public void setCity(String


c){city = c;}
(c) private int getAge(){return (d) Student(int a){age = a;}
age;}
(e) public void setName(String n){
name = n;}

39) Assume that one has written the following statements within the main method in the DriverProgram
class.

Student obj1 = new Student();


obj1.setName("Chamara Madushanka");
System.out.print(obj1.name);

What would the output of the program be?

(a) chamara madushanka (b) obj1.name (c) Student


(d) Obj1 (e) error

11
auroracs.lk
40) Assume that one has written the following statements within the main method in the DriverProgram class.
Now one can see only these statements within the main method.

Student obj1 = new Student(25); System.out.print(obj1.getAge());

What would the output of the program be?

(a) 25 (b) 0 (c) obj1.getAge()


(d) obj (e) error
41) Assume that one has again written the following statements within the main method in the DriverProgram
class. Now one can see only these statements within the main method.

Student obj1 = new Student(25);


obj1.setCity("Maharagama"); System.out.print(Student.city);

What would the output of the program be?

(a) 25 (b) Maharagama (c) Student.city


(d) Obj1 (e) error

42) Select from among the following, valid types of exceptions that can be seen in Java.

(a) Checked (b) Run time (c) Unchecked


(d) Design time (e) Compile time

43) Consider the following program written in Java.


public class Ex42{
public static void main(String args[]){
int a=8; System.out.println(a/0);
}
}

What would the output of the program be?

(a) NullPointerException (b) IOException


(c) ArrayoutofboundException (d) ArithmeticException
(e) NumberFormatException

44) Select from among the following the package in which the majority types that make up the
collection framework are defined.

(a) applet (b) net (c) swingx


(d) awt (e) util

45) The notations used for algorithm specification must conform to a specific set of criteria. Select from
among the following, the valid option(s) that can be considered as criteria for algorithm
specifications.

(a) It must be concise.


(b) It must be unambiguous
(c) It must be capable of machine execution.
(d) It must promote elegance in the solution.
(e) It must be dependent on Java programming language.

***********

12
auroracs.lk
Bachelor of Information Technology
Academic Year 2012/2013 – 1st Year Examination – Semester 2
IT2204 – Answers
Year 2013

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


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

IT2204 - Programming I
28th July, 2012
(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
auroracs.lk
1) Consider the following streams of bits noting the blank indicated as BLANK. A bitwise
operator which is denoted as BLANK applies to the input bit stream and then the output bit
stream is generated.

Input Bit Stream Operator Output Bit Stream


01101100 BLANK 10010011

Select from among the following, the valid operator which has been applied for the above
operation.

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


(d) >> 1 (e) << 1

2) Consider the following two numbers and bitwise operator indicated as BLANK. When the
operator was applied as number1 operator number2, the following output number has been
generated.

Number1 Operator Number2 Output


5 BLANK 3 1

Select from among the following, valid operator which has been applied for the operation.

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


(d) >> 1 (e) << 1

3) Consider the following number and bitwise operator indicated as BLANK. When the
operator was applied as number operator, the following output number has been generated.

Number Operator Output


3 BLANK 6

Select from among the following, valid operator which has been applied for the operation.

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


(d) >> 1 (e) << 1

4) Consider the following two numbers and bitwise operator indicated as BLANK. When the
operator was applied as number1 operator number 2, the following output number has been
generated.

Number1 Operator Number2 Output


5 BLANK 3 7

Select from among the following, valid operator which has been applied for the operation.

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


(d) >> 1 (e) << 1

2
auroracs.lk
5) Consider the following number and bitwise operator indicated as BLANK. When the
operator was applied as number operator, the following output number has been generated.

Number Operator Output


3 BLANK 1

Select from among the following, valid operator which has been applied for the operation.

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


(d) >> 1 (e) << 1

6) Select from among the following, valid operator(s) which can be categorized as unary type.

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


(d) -- (e) ++

7) Select from among the following, valid option(s) which can be considered keywords in Java.

(a) false (b) static (c) enum


(d) byte (e) void

8) Select from among the following, the company which bought the ownership of Java in 2010.

(a) Apple (b) Oracle (c) Sun Microsystems


(d) Microsoft (e) Intel

9) Consider the following program written in Java.

public class FirstProgram{


public static void main(String args[]){

System.out.println("Chamara Madushanka\n");
}
}

Select from among the following, incorrect statement(s) related to the given program.

(a) The given program can be written in any text editor and execute.
(b) White spaces which appear in the program are ignored by the compiler.
(c) main() method composed of the words namely main, String and args,
(d) \n represents single line comments in Java.
(e) Program should be saved by giving the file name Chamara Madushanka.

3
auroracs.lk
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 x = 0,y = 5,c = 4;
char ch = 'A'; // note that the ASCII value of A is 65

Select from among the given options, the correct output for each of the questions 10 – 14.

10) System.out.println(x = ++y);

(a) 3 (b) 4 (c) 5


(d) 6 (e) error

11) System.out.println(++ch);

(a) A (b) B (c) 65


(d) 66 (e) error

12) System.out.println(ch++);

(a) A (b) B (c) 65


(d) 66 (e) error

13) System.out.println(x++ + ++x + y++ + c++);

(a) 5 (b) 10 (c) 9


(d) 11 (e) error

14) System.out.println(y % c + ch);

(a) 66 (b) A (c) 65


(d) B (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. Answers for each option of questions 15 to 20 is given as a list of identifier numbers
indicating the program statements.

Identifier Java statements/Curly Brackets


1 {
2 }
3 final int kMConvertor = 1.609.0F;
4 final int yardsDivisor = 1760.0F;
5 int miles = 26.0F;
6 int yards = 385.0F;
7 double MarathonInKM = (miles * 1.609 + yards /
1760);
8 final float PI = 3.14F;
9 int radius = 5;
10 float areaOfCircle = PI * radius * radius;
11 System.out.println(areaOfCircle);
12 int radius = 5.0F;

4
auroracs.lk
13 int p = 100;
14 float r = 0.05F;
15 float r = 5%; int p = “Rs.” +100;
16 int t = 1;
17 float i = p * r * t;
18 System.out.println("Rs."+ i);
19 int month = 2;
20 case 12:
case 1:
case 2:
21 case 3:
case 4:
case 5:
22 case 6:
case 7:
case 8:
23 case 9:
case 10:
case 11:
24 break;
25 switch ( month )
26 System.out.println("Winter");
27 System.out.println("Spring");
28 System.out.println("Summer");
29 System.out.println("Autumn");
30 while(t <= 10 )
31 System.out.println(t);
32 t++;
33 int num1 = 1; int num2 = 7;
34 if( num1 < num2)
35 System.out.println(num1);
36 System.out.println(num2);
37 double MarathonInKM = (miles + yards /
yardsDivisor) * kMConvertor;
38 final float kMConvertor = 1.609F;
39 final float yardsDivisor = 1760.0F;
40 float miles = 26.0F;
41 float yards = 385.0F;
42 double MarathonInKM = (miles + yards / 1760) *
1.609;
43 System.out.println(MarathonInKM);
44 else
45 while(t >= 10 )
46 if( num1 > num2)
47 int p = 100F;
48 float final kMConvertor = 1.609.0;
49 float final yardsDivisor = 1760.0;
50 System.out.println(“MarathonInKM”);

5
auroracs.lk
15) A marathon is a long-distance foot race with an official distance of 42.18596875 kilometers (or 26
miles 385 yards, or exactly 26 7⁄32 miles). Write a Java program to convert the distance of a
marathon in miles and yards to kilo meters and output the kilo meter value. Miles can be converted
to kilo meters by multiplying it with 1.609. Yards can be converted to miles by dividing it with
1760. Miles and yards can be assigned to variables with the names miles and yards respectively.

The blank space in the following Java program is to be filled.

public class MarathonConvertor{


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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 40,41,42,43 (b) 2,3,4,5,6,43


(c) 38,39,40,41,42,43 (d) 38,39,40,41,37,43
(e) 3,4,40,41,33,43

16) Write a Java program to calculate the area of a circle. The formula for calculating area of a circle is
= π • radius of the circle² (or PI * r²) assuming the PI value as 3.14. Consider that the radius as 5.

The blank space in the following program is to be filled.

public class AreaOfACircle{


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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 8,9,10,18 (b) 8,9,10,11


(c) 3,4,5,6,7,11 (d) 8,12,9,10
(e) 43,42,41,40

17) Write a Java program to calculate the simple interest. The simple interest calculation formula is
I=P * r * t. (I = interest, P = Principle value, r = rate and t represents time). For example, if one
invest Rs.100 (the Principal) at a 5% annual rate for 1 year then the simple interest calculation is:
Rs.5 = Rs.100 x 5 % x 1 yr. One has to use the same values and variable in the program.

The blank space in the following program is to be filled.

public class SimpleInterest {


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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 13,14,15,16,17,18 (b) 13,14,15,16,17


(c) 13,14,16,17,18 (d) 13,14,15,16
(e) 13,14,15,17,18

6
auroracs.lk
18) Write a Java program print number series from 1 to 10 in the console using while loop. One has to
initialize the control variable with the name t equals to 1.

The blank space in the following program is to be filled.

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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 16,30,1,31 (b) 16,30,31,32


(c) 16,30,1,31,32,2 (d) 16,30,1,31,32,2,10
(e) 16,1,31,32,2

19) Write a Java program using switch statement to determine which season a particular month is in if
the month is given as a number (1 for January, 2 for February etc.).
Months 12,1,2  Season =Winter
Months 3,4,5  Season =Spring
Months 6,7,8  Season =Summer
Months 9,10,11  Season =Autumn
Otherwise  Bogus month
In the program there should be a variable having the name month with the value 2.

The blank space in the following program is to be filled.

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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 19,25,1,20,24,26,24,21,24,27,24,22,24
(b) 13,14,15,17,18,19
(c) 19,25,1,20,24,26,24,21,24,27,24,22,24,28,24,23,24,29,24,2
(d) 19,25,1,20,26,24,21,27,24,22,28,24,23,29,24,2
(e) 13,14,15,17,18,20

20) Write a Java program to find the smallest number out of given two numbers. Numbers should be
whole numbers with names num1 and num2. Variable should be filled with literals 1 and 7. Using if
statement one has to write the program.

The blank space in the following program is to be filled.

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

Which of the following list(s) of identified numbers from the given Java program segments could
be selected for the blank space?

(a) 33,34,23,35,44,36,3,4 (b) 33,34,35,44,36


(c) 33,34,1,35,2,44,1,36,2 (d) 33,34,23,35,44,36
(e) 23,35,44,36,3,4

7
auroracs.lk
Consider the following paragraph to answer questions 21 – 22.

“In the early days of programming, the main preoccupation was with the correctness of the solution.
Precisely how that solution was derived was very much subordinate to the actual solution itself”

21) Select from among the following, the era which has been described by the given paragraph.

(a) Programming as an art form (b) Modular programming


(c) Structures Programming (d) Object oriented Programming
(e) Java programming

22) Select from among the following, the design tool(s) which was/were emerged during this period.

(a) Flow charts (b) if– then – else construct


(c) Nassi – shneiderman (NS) diagram (d) Decision trees
(e) Pseudo code

23) Select from among the following, which can be considered as guiding principles for the
decomposition of a program into sub-procedures.

(a) Understandability (b) Clear identification of tasks


(c) Possibility of parallel development (d) Eliminate duplication of coding
(e) Code reusability

24) Select from among the following, correct statement on Java programming language.

(a) Java is a fully object oriented programming language.


(b) Java supports multiple inheritance.
(c) Memory management in Java is automatic.
(d) Usage of pointers is allowed in Java.
(e) Error handling in Java is inbuilt to the programming environment.

25) Select from among the following, the valid class/es which is/are bundled with java.lang package in
Java.

(a) Math (b) String (c) System


(d) Applet (e) IOExcepton

26) Consider the following output which has been generated when a program was executed.

java.lang.ArrayIndexOutOfBoundsException: 3

Select from among the following, possible option(s), which can be (a) candidate(s) to generate such
an output.

(a) int a[] = new int[2]; System.out.println(a[3]);


(b) file = new FileInputStream(fileName); x = (byte) file.read();
(c) int a[] = new int[2]; System.out.println(a[0]);
(d) int a[] = {2,3}; System.out.println(a[3]);
(e) int a[] = new int[2]; System.out.println(a[2]);

8
auroracs.lk
Consider the following diagram to answer questions 26 – 35 noting identifiers given for each
box. Assume that the diagram illustrates a hierarchy of classes and their relationships.

B C D E

27) Select from among the following, generic names in general, which can be given to identify the class
having the identifier A.

(a) Super class (b) Base class (c) Parent class


(d) Child class (e) Derived class

28) Select from among the following, generic names which can be given to identify the class having the
identifier C when consider the relationship with the class A.

(a) Child class (b) Derived class (c) Super class


(d) Parent class (e) Base class

29) Select from among the following, legal class declaration(s) which can be expected.

(a) class A extends B (b) class B extends A (c) class C extends A


(d) class D extends A (e) class A extends C

30) Select from among the following, valid relationship(s) in Java that are shown in the diagram.

(a) between A and B (b) between A and C (c) among A, C and D


(d) between A and E (e) among C, D and F

31) When consider the features of object orientation, which feature(s) is/are shown clearly in the
diagram?

(a) Inheritance (b) Method Overloading (c) Method Overriding


(d) Data abstraction (e) Multithreading

32) Select from among the following, suitable example(s) which can be given for class A.

(a) Employee (b) Doctor (c) CommissionEmployee


(d) Chamara Madushanka (e) Vehicle

33) Select from among the following, suitable example(s) which can be given for class B.

(a) Lorry (b) Book (c) ReferenceCopy


(d) Vehicle (e) Hospital

9
auroracs.lk
34) Consider that the class A has been applied with the modifier abstract in a class declaration. Select
from among the following, option(s) which are not allowed.

(a) A(){} (b) abstract void show();


(c) class B extends A (d) A obj1 = new A();
(e) obj1.show();

35) Consider that the class C has been applied with the modifier final in a class declaration. Select from
among the following, option(s) which are not allowed.

(a) C(){} (b) abstract void update();


(c) class F extends C (d) C obj2 = new C();
(e) private int var1;

36) Consider the following method signatures written in the class B.

B ()
B(int a, int b)

Select from among the following, object orientation feature(s) used in the above class.

(a) Inheritance (b) method Overloading (c) Function Overriding


(d) Data abstraction (e) Multithreading

37) Select from among the following, the common name which can be given for method overloading
and method overriding as a whole.

(a) Polymorphism (b) Abstraction (c) Information hiding


(d) Encapsulation (e) swings

38) Select from among the following, the keywords which can be used implement in Java the object
orientation feature called information hiding.

(a) public (b) main (c) private


(d) new (e) protected

39) Consider the following table with some random values. One needs to keep those values in a
computer to process them.

2 5 6
6 17 2

Select from among the following, valid way(s) of keeping data in a computer by using Java.

(a) int a =2,b=5,c=6,d = 6, e = 17, f = 2;


(b) int ar = { 3,5,6},{6,17,2};
(c) int ar []= { 3,5,6},{6,17,2};
(d) int ar[][] = {{ 3,5,6},{6,17,2}};
(e) int[][] ar = {{ 3,5,6},{6,17,2}};

10
auroracs.lk
40) Consider the following skeleton of program noting the blanks indicated as BLANK1, BLANK2
and BLANK3.

try
{
BLANK1
}catch(BLANK2)
{
BLANK3
}

Then consider the following candidate phrases which can be used to fill those blanks.

I protected code
II Exception object
III Catch block
IV Stream buffer

Select from among the following, (a) valid option(s) which can be consider for filling the blanks.

(a) BLANK1  I, BLANK2  II, BLANK3  III


(b) BLANK1  II, BLANK2  III, BLANK3  I
(c) BLANK1  IV, BLANK2  II, BLANK3  I
(d) BLANK1  I, BLANK2  III, BLANK3  II
(e) BLANK1  II, BLANK2  IV, BLANK3  I

41) Select from among the following, valid classes which comes with java.util package supporting set
collection.

(a) HashSet<T> (b) LinkedHashSet<T> (c) TreeSet<T>


(d) Stack<T> (e) Vector<T>

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

int ar1[]={1,2,3,4,5}; int ar2[]=new int[5];


for(int i = 0,j = 4;i < 5 & j>=0;i++,j--)
ar2[i] = ar1[i]+ar1[j];
for(int i = 0; i < 5 ;i++ )
System.out.print( ar2[i] );

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

(a) 12345 (b) 54321 (c) 246810


(d) 66666 (e) error

43) Select from among the following, standard streams that are accessible through members of System
class in Java.

(a) standard input (b) standard output (c) standard error


(d) Exception (e) BufferedReader

11
auroracs.lk
44) Read the following statement which explains one of the subclasses of FilterInputStream class.

“Read data from an encrypted input stream.”

Select from among the following, the sub class which matches with the above description.

(a) AudioInputStream (b) DigestInputStream (c) DateInputStream


(d) CipherInputStream (e) PipedInputStream

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

String firstString = "Chamara";


String secondString = " madushanka";
System.out.println(firstString += secondString);

What would the output of the program be?

(a) Chamara madushanka (b) += (c) madushanka


(d) Chamara (e) error

*********

12
auroracs.lk
Bachelor of Information Technology
Academic Year 2011/2012 – 1st Year Examination – Semester 2
IT2204 – Programming I - Answers
Year 2012

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
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.

auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
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
auroracs.lk
Bachelor of Information Technology
Academic Year 2010/2011 – 1st Year Examination – Semester 2
IT2204 – Programming I - Answers
Year 2011

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk
UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY


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

IT2204 - Programming I
07th August, 2010
(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 13 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
auroracs.lk
1) Select from among the following, the correct options which can be considered as packages
that come under the Java standard library.

(a) java.lang (b) java.io (c) java.util


(d) java.awt (e) java.swing

2) Consider the following name which is appearing in the home directory of a windows
environment.

jdk1.5.0_01

Select from among the following, the release version number which can be seen in the
above directory name.

(a) jdk (b) 1 (c) 5


(d) 0 (e) 01

3) Select from among the following, the file name/s which can be qualified as (a) Java source
file(s).

(a) class.java (b) Customer.java (c) Customer.class


(d) Employee.java (e) Employee.class

4) Which of the following options is/are correct on valid statements in Java?

(a) for( x>34 ; int a ; a++); (b) float newValue = 35.5;


(c) int value = value + 1; (d) String myFriend = “Vimukthi”;
(e) import java.applet.*;

5) Select from among the following, what can be considered as key words available in Java.

(a) enum (b) return (c) case


(d) args (e) int

6) Consider the following statement.

“A programmer needs to avoid the execution of some codes from the program.”

Select from among the following, the correct operator(s) which is/are valid in Java to achieve the
wish of the programmer.

(a) // (b) % (c) $


(d) if (e) /* /**

2
auroracs.lk
Use the following declarations and initializations to evaluate the Java expressions given in
questions 7 - 11. Assume that each expression is evaluated separately in the program.
long x = 10,y = 15,z = 20;
int m = 0;
short k = 10;
char ch = 'B' // note that the ASCII value of B is 66

7) System.out.println(m = x);

(a) false (b) 10 (c) 10.0


(d) 100 (e) error

8) System.out.println(k = k * 100);

(a) 10 (b) 1 (c) 100


(d) 1000 (e) error

9) System.out.println(m = ch);

(a) ch (b) m (c) 66


(d) B (e) error

10) System.out.println(x = y % z);

(a) 10 (b) 15 (c) 20


(d) 0 (e) error

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

(a) 66 (b) 10 (c) 76


(d) 8 (e) error

Consider the following program written in Java to answer questions 12 – 20.

class A{
private int var1;
private float value1 = 34.5f;
private static value2;

A(c){
value2 = c;
}

public int setVar1(int x){


x = var1;
}

public void getVar1(){


return var1;
}

3
auroracs.lk
12) When the program was compiling, errors were generated showing the following messages in the
command prompt.

Select from among the following, the programming statement/s which could be the cause/s for the
error generated indicated as the error number 5.

(a) return var1; (b) private float value1 = 34.5f;


(c) A(c) (d) private static value2;
(e) private int var1;

13) In order to correct the error generated, statements in the program can be replaced with new
statements as shown in the following table.

Row Existing statement New statement


number
1 return var1; System.out.println(value2);
2 private float value1 = 34.5f; private float value1;
3 A(c) A(int a)
4 private static value2; private static long value2;
5 private int var1; public int var1;

Select from among the following, the correct row number/s which is/are suitable for eliminating the
error.

(a) 1 only (b) 2 and 3 only


(c) 3 only (d) 4 only
(e) 5 only

4
auroracs.lk
14) After successfully correcting the error in question number 12, the program was compiled again. The
following message was displayed in the command prompt.

Select from among the following, the programming statement/s which could be the cause/s for the
error generated.

(a) return var1; (b) private float value1 = 34.5f;


(c) A(c) (d) private static value2;
(e) private int var1;

15) In order to correct the error generated in the question number 14, statements in the program can be
replaced with new statements as shown in the following table.

Row Existing statement New statement


number
1 return var1; System.out.println(value2);
2 private float value1 = 34.5f; private float value1;
3 A(c) A(int a)
4 private static value2; private static long value2;
5 private int var1; public int var1;

Select from among the following, the correct row number/s which is/are suitable for eliminating the
error.

(a) 1 only (b) 2 only (c) 3 only


(d) 4 only (e) 4 and 5 only

16) After correcting the erroneous code traced in question number 15, the program was compiled again.
The following message was displayed in the command prompt showing an error.

Select from among the following, the programming statement/s which could be the cause/s for the
error generated indicated as the error number 16.

(a) return var1; (b) public int setVar1(int x)


(c) A(c) (d) x = var1;
(e) private int var1;

5
auroracs.lk
17) In order to correct the error generated in question number 16, statements in the program can be
replaced with new statements as shown in the following table.

Row Existing statement New statement


number
1 return var1; System.out.println(value2);
2 public int setVar1(int x) public void setVar1(int x)
3 A(c) A(int a)
4 x = var1; x = int var;
5 private int var1; public int var1;

Select from among the following, the correct row number/s which is/are suitable for eliminating the
error.

(a) 1 only (b) 2 and 3 only (c) 3 only


(d) 4 only (e) 4 and 5 only

18) After correcting the erroneous code traced in question number 17, the program was compiled again.
The following message was displayed again in the command prompt indicating an error.

Select from among the following, the programming statement/s which could be the cause/s for the
error generated.

(a) return var1; (b) public int setVar1(int x)


(c) A(c) (d) x = var1;
(e) private int var1;

19) In order to correct the error generated in question number 18, statements in the program can be
replaced with new statements as shown in the following table.

Row Existing statement New statement


number
1 return var1; System.out.println(value2);
2 public int setVar1(int x) public void setVar1(int x)
3 A(c) A(int a)
4 x = var1; x = int var;
5 private int var1; public int var1;

Select from among the following, the correct row number/s which is/are suitable for eliminating the
error.

(a) 1 only (b) 2 only (c) 2 and 3 only


(d) 4 only (e) 4 and 5 only

6
auroracs.lk
20) After debugging the erroneous situation in question number 19, the program was complied
successfully. But still, one can see a problematic code in the program which can affect the proper
execution of the program. Consider the following situation regarding the existing statement and the
new statement.

Row Existing statement New statement


number
1 return var1; System.out.println(value2);
2 public int setVar1(int x) public void setVar1()
3 A(c) A(int a)
4 x = var1; var1 = x;
5 private int var1; public int var1;

Select from among the following, the correct row number/s which is/are suitable for eliminating the
error.

(a) 1 only (b) 2 only (c) 2 and 3 only


(d) 4 only (e) 4 and 5 only

21) Select from among the following, the valid array declarations that are allowed in Java.

(a) int ar[]= new ar[]; (b) int[] ar= new int[7];
(c) int ar = new int[85] ; (d) int ar[] = new int[];
(e) int ar[45] = new ar[];

22) Consider the following program written in Java.

public class ProblemOne{


public static void main(String args[]){
char ar[][]= {{'v','a','b','c'},
{'x','i','j','k'},
{'z','x','m','y'},
{'l','m','n','u'}};

for(int x=0 ; x<ar.length ; x++){


for(int y=x ; y<x+1 ; y++)
System.out.print(ar[x][y]);
}
}

What would the output be when the program is executed?

(a) vabc (b) vimu (c) cjxl


(d) lmnu (e) ckyu

7
auroracs.lk
Use the following declarations and initializations to evaluate the Java expressions given in
questions 23 - 24. Assume that each expression is evaluated separately in the program.

byte a = 10;
float y = 5.0f;
int m = 55;

23) System.out.println(a * m % y);

(a) 10 (b) 5.0 (c) error


(d) 50.0 (e) 0.0

24) System.out.println(a>m && y==a/0 ? a*a : y);

(a) false (b) 100 (c) 5.0


(d) true (e) error

25) Consider the following segment of a Java program.

int year = 2004;


if((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0))

System.out.println( year + " is a leap year");

else

System.out.println( year + " is not a leap year");

What would be the intended output of the program?

(a) year + " is a leap year" (b) year + " is not a leap year"
(c) 2004 + " is a leap year" (d) 2004 is a leap year
(e) 2004 is not a leap year

26) Consider the following segment of a Java program.

int ar[] = new int[]{1,2,3,4,5};

for(int i=0; i < ar.length ; i ++){

if(ar[i] == 3)

break;

else

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

What would be the intended output of the program?

(a) 1245 (b) 345 (c) error


(d) 12 (e) 15

8
auroracs.lk
27) Consider the following segment of a program written in Java.

int num1 = 10;


int num2 = 20;

System.out.print(num1+" "+num2+" ");

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

System.out.println(num1+" "+num2);

What would be the output of the program if it is executed successfully?

(a) 10 10 10 10 (b) 10 20 10 20 (c) 10 20 20 10


(d) 10 10 20 20 (e) 20 20 20 20

28) Consider the following program written in Java.

public class Problem{


public static void main (String args[]) {

int value = -1;


do {
if (value == -1){
System.out.print("Vimukthi ");
value++;
}
else {
System.out.print("Jayaweera");
System.out.print(" ");
value--;
}
value += 2;
} while (value < 3);
}

What would be the output of the program?

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


(c) Vimukthi Jayaweera (d) Vimukthi
(e) Jayaweera Vimukthi

9
auroracs.lk
29) Consider the following program written in Java.

class WhileDemo {
public static void main(String[] args){
int count = 5;
while (count > 3) {
System.out.print(count);
count--;
}
}
}

What would be the output of the program?

(a) 12345 (b) 54321 (c) 12


(d) 54 (e) 123

Consider the following program written in Java to answer questions 30 – 32,

public class Switch{


public static void main(String args[]){
int number = 4;
switch(number < 7){
case 1 : System.out.println("One");
case 2-5: System.out.println("between two and five");
case 6 : System.out.println("Six");
}
}
}

30) Select from among the following, statement/s which can be seen in the program that can be
considered as not valid in Java.

(a) public class Switch (b) switch(number < 7)


(c) case 2-5: System.out.println("between two (d) case 6 : System.out.println("Six");
and five");
(e) case 1 : System.out.println("One");

31) Select from among the following, the key words which cannot be seen in the program but required
for the completeness of it.

(a) catch (b) return (c) default


(d) while (e) break

32) When the above program was complied, some errors were reported in the command prompt. Select
from among the following, the cause/s for those errors generated. They were generated due to

(a) missing catch key word. (b) missing while key word.
(c) the case 2-5: programming code (d) the public class Switch statement.
(e) the switch(number < 7) code

10
auroracs.lk
33) Select from among the following, the correct option/s that can be considered as common
characteristics of sub routines and functions.

(a) They are invoked by stating their names together with any required parameters.
(b) Sub routines and functions may call themselves.
(c) They may themselves call other sub-procedures, but may not call the main procedure.
(d) Once they are called, their results are communicated as global data or passed parameters.
(e) When they are invoked, the program control passes to the called sub procedure and
resumes at the first executable statement in that procedure.

34) There are some guiding principles introduced for decomposition of a program into sub procedures
and eliminating duplication of codes is one such a principle. Select from among the following, the
correct statement/s which describe/s the need of eliminating duplication of codes.

(a) It provides the ability for original programmer or others who follow him to be able
to understand the nature of the program
(b) No task should be coded in more than one place in any program.
(c) Divide the project into small self-contained procedures and allocate procedures among the
programmers to develop.
(d) After decomposition each procedure should perform one and only one task.
(e) Process which has already been coded and known to work correctly in one program can be
copied into another program which requires the same functionality.

35) Consider the following statement noting the blank space.

____________________is a program design tool which approximates the format of a programming


language and is therefore easier to be translated into a program.

Select from among the following, the correct option which can be used to fill the blank.

(a) Nassi-Shneidernam diagram (b) Flow charts


(c) Pseudocode (d) Object Oriented Design
(e) Structures Design

36) Select from among the following, the package in which most of the types that make up the
collection framework are stored.

(a) java.lang (b) java.net (c) java.util


(d) java.awt (e) java.math

37) Consider the following programming statements written in Java.

String str1=”Vimukthi”;
String str2+=” likes playing wrestling”;

What would be the content inside the variable str2?

(a) Vimukthi (b) likes playing wrestling


(c) Vimukthi likes playing wrestling (d) error
(e) likes playing Vimukthi wrestling

11
auroracs.lk
38) Consider the following program written in Java.

class Str{
public static void main(String args[]){
double value1 = 10000;
float value2 = 35.0f;
String str = String.valueOf(value1);
System.out.println(str + value2);
}
}

What would be the output of the program?

(a) error (b) 10000 (c) 35.0


(d) 10035.0 (e) 10000.035.0

39) Consider the following program written in Java.

class Abc{
public static void main(String args[]){
String str1 = "Anuradhapura";
String str2 = "Anuradhapura";
System.out.print(str1.equals(str2));
}
}

What would be the output of the program?

(a) error (b) false (c) true


(d) Anuradhapura (e) Anu

40) Consider the following program written in Java.

class Compare{
public static void main(String args[]){
String str1 = "Sigiriya";
String str2 = "Jaffna";
System.out.print(str1.compareTo(str2));
}
}

What would be the output of the program?

(a) true (b) false (c) error


(d) 9 (e) Sigiriya

41) Select from among the following, key words which have a direct relationship with the object
oriented concept called data hiding.

(a) private (b) protected (c) public


(d) default (e) main

42) Select from among the following, the stream type/s which is/are supported by the java.io package.

(a) logical (b) binary (c) output


(d) character (e) physical

12
auroracs.lk
43) FilterInputStream class has nine direct subclasses that provide more specialized ways of
transforming data from an input stream. The following statement gives a description to one such
class inherited from the class FilterInputStream.

“It reads data of primitive types from a binary stream.”

Select from among the following, the correct subclass which matches with the above description.

(a) BufferedInputStream (b) DataInputStream


(c) CheckedInputStream (d) CipherInputStream
(e) DigestInputStream

44) Select from among the following, different items of information that can be expected from an
object of type throwable class about an exception.

(a) A message which has been initialized by a constructor denoting the exception
(b) A way to clean up garbage generated at the end of executing a try block
(c) A record for the execution stack at the time the object was created
(d) A message informing a standard exception after rethrowing and object of type throw
(e) A message informing a wrong usage of an input device

45) Consider the following message which can be expected in the command prompt when a program
statement is executed.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:


String index out of range: 22

Select from among the following, the programming statements which can be cause the generator of
such a message in the command prompt.

(a) String str1 = "University of Colombo"; (b) for(int i=0;i<ar.length;i++)


String str2 = str1.substring(20,22); int sum+=ar[i];
(c) int value = num3 / 0; (d) System.out.println(“ My number “ + no);
(e) float value = 34.99;

*********

13
auroracs.lk
Bachelor of Information Technology
Academic Year 2009/2010 – 1st Year Examination – Semester 2
IT2204 – Programming I - Answers
Year 2010

1 a b c d e 21 a b c d e 41 a b c d e

2 a b c d e 22 a b c d e 42 a b c d e

3 a b c d e 23 a b c d e 43 a b c d e

4 a b c d e 24 a b c d e 44 a b c d e

5 a b c d e 25 a b c d e 45 a b c d e

6 a b c d e 26 a b c d e 46 a b c d e

7 a b c d e 27 a b c d e 47 a b c d e

8 a b c d e 28 a b c d e 48 a b c d e

9 a b c d e 29 a b c d e 49 a b c d e

10 a b c d e 30 a b c d e 50 a b c d e

11 a b c d e 31 a b c d e 51 a b c d e

12 a b c d e 32 a b c d e 52 a b c d e

13 a b c d e 33 a b c d e 53 a b c d e

14 a b c d e 34 a b c d e 54 a b c d e

15 a b c d e 35 a b c d e 55 a b c d e

16 a b c d e 36 a b c d e 56 a b c d e

17 a b c d e 37 a b c d e 57 a b c d e

18 a b c d e 38 a b c d e 58 a b c d e

19 a b c d e 39 a b c d e 59 a b c d e

20 a b c d e 40 a b c d e 60 a b c d e

http://www.ucsc.lk http://www.bit.lk

auroracs.lk

You might also like