BIT 243 - Java Programming - Mid - Exam
BIT 243 - Java Programming - Mid - Exam
BIT 243 - Java Programming - Mid - Exam
INSTRUCTION TO CANDIDATES:
Page 1
SECTION A (1 X 30 = 30 MARKS)
Select the correct Answer.
1. Which of the following option leads to the portability and security of Java?
a. 0-9
b. A-Z, a-z
c. $, _ (Underscore)
d. All the above
class Test{
public static void main(String[] args) {
String System="Variable";
System.out.println(System);
}
}
a. Variable
b. Compile-time error
Page 2
c. Exception
d. None of these
6. What is the output of the below Java program with WHILE, BREAK and CONTINUE?
class Numbers
{
public static void main(String args[])
{
int a=20, b=10;
if ((a < b) && (b++ < 25))
{
System.out.println("This is any language logic");
}
System.out.println(b);
}
}
Page 3
a. 10
b. 11
c. 12
d. Compilation Error
class evaluate
{
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);
}
}
a. 38
b. 39
c. 40
d. 41
9. Which of the following code segments will produce the displayed output?
1
22
333
4444
55555
Page 4
III. for (int i = 1; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(i);
}
System.out.println();
}
a. I
b. II
c. III
d. IV
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
a. 0 2 4 6 8
b. 1 3 5 7 9
c. 0 1 2 3 4 5 6 7 8 9
d. 1 2 3 4 5 6 7 8 9 10
Page 5
12. What are the values of var1 and var2 after the following code segment is executed and the
while loop finishes?
class t
{
public static void main(String[] args)
{
int var1 = 0;
int var2 = 2;
while ((var2 != 0) && ((var1 / var2) >= 0))
{
var1 = var1 + 1;
var2 = var2 - 1;
}
System.out.println("Var1="+var1+",Var2="+var2);
}
}
a. var1 = 0, var2 = 2
b. var1 = 1, var2 = 1
c. var1 = 3, var2 = -1
d. var1 = 2, var2 = 0
class box
{
int width;
int height;
int length;
int volume;
void volume(int height, int length, int width)
{
volume = width*height*length;
}
}
class Prameterized_method
{
public static void main(String args[])
{
box obj = new box();obj.height = 1;
obj.length = 5;
obj.width = 5;
obj.volume(3,2,1);
System.out.println(obj.volume);
}
Page 6
}
a. 0
b. 1
c. 6
d. 25
14. What is the output of the below Java program with a final local variable?
a. 30
b. 20
c. 10
d. Compiler error
a. Encapsulation
b. Polymorphism
c. Exception
d. Abstraction
class TIH
{
TIH ( ) { }
}
a. method
b. constructor
c. Both
Page 7
d. None of the above
17. Choose the correct way of creating an object of the below class.
class Table
{
Table()
{
System.out.println("Table Created");
}
}
18. Select the correct output for the below code snippet:
class abc
{
int i;
public int a(int x)
{
i=x+1;
System.out.println("Value of i="+i);
return i;
}
}
public class xyz
{
abc obj1,obj2;
public xyz()
{
obj1=new abc();
obj2=obj1;
obj2.a(3);
}
public static void main(String[] args)
{
xyz x=new xyz();
}
}
Page 8
c. Print on the console: 'value of i=3’
d. Print on the console: 'value of i=5’
20. What is the process of defining two or more methods within same class that have same
name but different parameters declaration?
a. method overloading
b. method overriding
c. method hiding
d. none of the mentioned
a. Methods
b. Constructors
c. Classes and methods
d. Methods and Constructors
class overload
{
int x;
int y;
void add(int a)
{
x = a + 1;
}
void add(int a , int b)
{
x = a + 2;
}
}
class Overload_methods
{
public static void main(String args[])
{
overload obj = new overload();
int a = 0;
Page 9
obj.add(6, 7);
System.out.println(obj.x);
}
}
a. 6
b. 7
c. 8
d. 9
class Counter2
{
static int count=0;
Counter2()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
a. 0
0
0
b. 1
1
1
c. 1
2
3
d. Compile error
24. What is the main difference between a WHILE and a DO-WHILE loop in Java?
a. WHILE loop executes the statements inside of it at least once even if the condition is
false.
b. DO-WHILE loop executes the statements inside of it at least once even if the condition
is false.
c. WHILE loop is fast.
d. DO-WHILE loop is fast.
Page 10
25. What is the output of the below Java code with a FOR loop?
a. 1,2,3,4,
b. 1,2,3,5
c. 5,
d. 5
int time=50;
do
{
System.out.print(time + ",");
time++;
}while(time < 53);
a. 50,50,50,
b. 50,51,52,
c. 51,52,53,
d. Compiler error
class SimpleCalc
{
public int value;
public void calculate()
{
value +=7;
}
}
public class MultiCalc extends SimpleCalc
{
public void calculate()
Page 11
{
value -=3;
}
public void calculate(int multiplier)
{
calculate();
super.calculate();
value *= multiplier;
}
public static void main(String args[])
{
MultiCalc calculator = new MultiCalc();
calculator.calculate(2);
System.out.println("Value is: "+ calculator.value);
}
}
a. Value is: 8
b. Compilation fails
c. Value is : 12
d. Value is : -12
class demo
{
int a, b;
demo()
{
a = 10;
b = 20;
}
public void print()
{
System.out.println ("a = " + a + " b = " + b + "n");
}
}
class Test
{
public static void main(String[] args)
{
demo obj1 = new demo();
demo obj2 = obj1;
obj1.a += 1;
obj1.b += 1;
System.out.println ("values of obj1 : ");
Page 12
obj1.print();
System.out.println ("values of obj2 : ");
obj2.print();
}
}
a. Compile error
b. values of obj1:
a = 11 b = 21n
values of obj2:
a = 11 b = 21n
c. values of obj1:
a = 11 b = 21
values of obj2:
a = 10 b = 20
d. values of obj1:
a = 11 b = 20
values of obj2:
a = 10 b = 21
a. Initializing
b. Instantiating
c. Interfacing
d. None of the above
a. abstract class
b. public class
c. inner class
d. anonymous class
End of paper.
Page 13