Академический Документы
Профессиональный Документы
Культура Документы
Question 1
(a) Mention any two attributes required for class declaration.
Characteristics and behavior.
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.
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.
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.
{
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
ompu
4
Applicationster
false
(f) If array[] = {1, 9, 8, 5, 2};
(i) What is array.length?
5
(ii) What is array[2]?
8
char ch = in.readLine().charAt(0);
(ii) String
String s = in.readLine();