Sri Lanka Institute of Information Technology
B.Sc. Honors Degree in Information Technology
Mid-Term – Mock Paper
Year 1, Semester 1 (2024)
IT1120 – Introduction to Programming
Duration: 1 Hour
Instructions to Candidates:
• Paper has 25 questions
• The total mark for the paper is 100
• Each question has only 1 correct answer
• Each question carries equal marks
1. Find the output of the below code segment?
double interest = 0.0;
double bal = 10000.00;
char type = 'F';
if (type == 'A')
interest = bal * 0.25;
bal = bal + interest + bal * 0.1;
System.out.println(bal);
Select one:
a. 0.0
b. 1000.0
c. 3500.0
d. 13500.0
e. 11000.0
2. Which of the following X value will yield the below expression to ‘True’?
( X < (X + 1) ) && (X == 8)
Select one:
a. 10
b. 5
c. 1
d. 8
3. If the value of X is 1245, what is the output of the below statement?
int x = 12345;
System.out.print(x % 1000 + " ");
System.out.print(x % 100 + " ");
System.out.print(x / 10);
Select one:
a. 12.345 123.45 1234.5
b. 12 123 1234
c. 345 45 0.5
d. 345 45 1234
e. None of the above
2
4. Which of the following are correct variable names?
Select one:
a. total , 1num , average_mark , _total
b. total salary , average , num1
c. totalMark , salary , num_one , average
d. int , num , salary , total
e. mark , total , average , $count
5. Which of the following comments is used to add a Block-Line comment in Java ?
Select one:
a. */ This is a block-line comment /*
b. <!-- This is a block-line comment -->
c. /* This is a block-line comment */
d. // This is a block-line comment
6. What will be the output of the following code segment?
int sum = 15;
if (sum <= 20)
sum = sum + 5;
else
sum = sum - 5;
sum = sum + 15;
System.out.print("value = " + sum);
Select one:
a. value = 30
b. value = 15
c. value = 20
d. value = 10
e. value = 35
3
7. What will be the output of the following code segment?
int choice = 2;
switch(choice){
case 1+2 / 3:
System.out.print("Inside First Case\n");
break;
case 2 / 2 * 3:
System.out.print("Inside Second Case\n");
break;
default:
System.out.print("Inside Default Case\n");
}
Select one:
a. Inside First Case
b. Inside Second Case
c. Inside Default Case
d. Compile Error
8. What is the output of the following code segment ?
** Note: ASCII code of 'A' is 65
char ch;
int i;
ch = 'D';
i = ch - 'A';
System.out.println(i);
Select one:
a. 1
b. 2
c. 3
d. 4
e. 5
4
9. What is the output of the following code if the user inputs the following sequence of numbers:
5, 10, -3, -1
import java.util.Scanner;
public class SentinelLoopExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int sum = 0;
int anyNegativeNumber = -1;
System.out.println("Enter number :");
number = input.nextInt();
while (number != anyNegativeNumber) {
sum += number;
number = input.nextInt();
}
System.out.println("Sum: " + sum);
}
}
Select one:
a. Compile Error
b. Sum: 15
c. Sum: 12
d. Sum: 0
10. Consider the following code segment and evaluate the two statements below:
String result =
(number > 0) ?
((number % 2 == 0) ? "Positive Odd" : "Positive Even") :
((number < 0) ? "Negative" : "Zero");
System.out.print(result);
Statement A – If the number variable is 13 the above code prints: Positive Odd
Statement B – If the number variable is 0 the above code prints: Zero
Select one:
a. Both A and B are true
b. Both A and B are false
c. A is true, but B is false
d. A is false, but B is true
5
11. What is the output of the following Java code segment ?
int x = 5;
int y = (x++ > 5) ? ++x : x++;
System.out.println("x: " + x + ", y: " + y);
Select one:
a. x: 6, y: 6
b. x: 6, y: 5
c. x: 7, y: 6
d. x: 7, y: 5
12. What is the output of the following Java code segment ?
double d = 128.75;
int i = (int)(d / 2.0);
double j = (int)i;
System.out.println("d: " + d + ", i: " + i + ", j: " + j);
Select one:
a. d: 128.75, i: 64, j: 96
b. d: 128.75, i: 64, j: 64
c. d: 128.75, i: 64, j: 96.0
d. d: 128.75, i: 64, j: 64.0
Answers
1. 11000.0
2. 8
3. 345 45 1234
4. totalMark , salary , num_one , average
5. /* This is a block-line comment */
6. value = 35
7. Inside Default Case
8. 3
9. Sum: 12
10. A is false, but B is true
11. x: 7, y: 6
12. d: 128.75, i: 64, j: 64.0