Вы находитесь на странице: 1из 3

Section A (40 Marks)

Attempt all questions.

Question 1
(a) Mention any two attributes required for class declaration.
Characteristics and behavior.

(b) State the difference between token and identifier.


Token is the smallest individual unit in a program.
Identifiers are fundamental building blocks of a program and are used as the
general terminology for the names given to different parts of the program.

(c) Explain instance variable. Give an example.


A data member that is created for every object of the class is an instance
variable.
Example:

class Example{
int num; //instance variable
}
(d) What is inheritance and how is it useful in Java?
Inheritance is a mechanism that allows a class to derive properties from another
class. It is useful in Java because:
(i) It gives us the capability to express the inheritance relationship.
(ii) It allows reuse of code.

(e) Explain any two types of access specifier.


public makes the variables and methods visible to all classes.
private makes the variables and methods visible only within the class.

Question 2
(a) What is meant by an infinite loop? Give an example.
An infinite loop is a loop which keeps on executing repeatedly because its
terminating condition is either missing or is never reached.
Example:

while(true)
System.out.println("Google");
(b) State the difference between == operator and equals() method.
The == operator is a relational operator that is used to compare the contents of
primitive data type values for equality. The equals() method is a member of the
String class and is used to compare the contents of two String values for equality.

(c) Differentiate between actual parameter and formal parameter.


The parameters that appear in function call are called actual parameters. The
parameters that appear in function definition are called formal parameters.

(d) What is the use of exception handling in Java?


It separates error-handling code from normal code.
It makes the programs clear, robust and fault-tolerant.

(e) Differentiate between base and derived class.


A class from which another class is inheriting its properties is called base class.
The class inheriting properties from another class is called derived class.

Question 3
(a) Explain the function of each of the following with an example:
(i) break;
The break keyword is used to terminate a loop or a switch statement.
for(int i = 1; i <= 10; i++){
if(i % 5 == 0)
break;
System.out.println(i);
}
(ii) continue;
The continue keyword is used to skip the current iteration and moves the program
flow to the next iteration of the loop.

for(int i = 1; i <= 10; i++){


if(i % 3 == 0)
continue;
System.out.println(i);
}
(b) Convert the following segment into equivalent for loop:

{
int i, l = 0;
while(i <= 20)
System.out.print(i + " ");
l++;
}
{
int i, l = 0;
for(;l <= 20;)
System.out.print(i + " ");
l++;
}
(c) If a = 5, b = 9 calculate the value of a += a++ � ++b + a;
a = 6

(d) Give the output of the following expressions:


(i) If x = -9.99, calculate Math.abs(x); 9.99
(ii) If x = 9.0, calculate Math.sqrt(x); 3.0

(e) If String x = �Computer�;


String y = �Applications�;
What do the following functions return?
(i) System.out.println(x.substring(1, 5));
(ii) System.out.println(x.indexOf(x.charAt(4)));
(iii) System.out.println(y + x.substring(5));
(iv) System.out.println(x.equals(y));

ompu
4
Applicationster
false
(f) If array[] = {1, 9, 8, 5, 2};
(i) What is array.length?
5
(ii) What is array[2]?
8

(g) What does the following mean?

Employee staff = new Employee();


An object named �staff� is being created of class �Employee�.
(h) Write a Java statement to input/read the following from the user using the
keyboard.
(i) Character

char ch = in.readLine().charAt(0);
(ii) String

String s = in.readLine();

Вам также может понравиться