IT1406 - Introduction To Programming: University of Colombo, Sri Lanka

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

UNIVERSITY OF COLOMBO, SRI LANKA

UNIVERSITY OF COLOMBO SCHOOL OF COMPUTING

DEGREE OF BACHELOR OF INFORMATION TECHNOLOGY ( 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

You might also like