1.
(a.) Specify the most suitable data type to declare followings? [3 Marks]
i.) No of subjects in the course
ii.) Name of the lecturer
iii.) Average mark for a subject in a class
(b.) List all primitive data types [4 Marks]
(c.) Explain the purpose of following escape sequence characters? [3 Marks]
i.) \n
ii.) \t
iii.) \”
(d.) What will be the output of following program fragment? [2 Marks]
byte num=0;
num++;
++num;
System.out.println(num++);
(e.) Fill in the blanks of the following sentences with suitable terms. [4 Marks]
i.) The file produced by the java compiler contains………………..that are executed by the Java Virtual Machine.
ii.) Three parts in the method definition form …………………………...
iii.) A variable declared and initiated within a loop is known as …………
iv.) A class variable should be declared with a…………keyword before the type of variable.
(f.) Write a java program to compare two integer variables and display the largest
using with input and message dialog boxes. [4 Marks]
2.
(a.) State whether each of the following is true or false. [4 Marks]
i.) By convention, method names begin with an uppercase first letter and all subsequent words in the name begin with a capital first letter
ii.) Empty parentheses following a method name in a method declaration indicate that the method does not require any parameter to perform its task.
iii.) The number of argument in the method call must match the number of parameters in the method declaration’s parameter list.
iv.) An import declaration is not required when one class in a package uses another in the same package.
(b.) What is the different between local variable and a field? [2 Marks]
(c.) Explain the purpose of a method parameter. What is the difference between a
parameter and an argument? [2 Marks]
(d.) Write a complete Java application to prompt the user for the double radius of a
sphere, and call method sphereVolume to calculate and display the volume of the sphere. Use the Math.pow( radius) method and Math.PI constant of Math class. [4 Marks]
(e.) Write what would be the output of the following program if it is executed. [8 Marks]
class ResultPrint
{
public static void main(String[] args)
{
int x1 = 5, x2 = 6, x3 = 4, x4 = 7;
System.out.println((x1^2+3*x3)/2);
System.out.println( x1 == 5);
System.out.println( x2!=6 );
System.out.println( x3!= 5 && x4 < 8);
System.out.println( x1<= 3 & x2 > x3);
System.out.println( x2 >= x3|| x1 != x4);
System.out.println( x1 + x2 - x3> x4 | 4*x1>= (x3^2));
System.out.println( x4 != x3+x2 );
}
}
3.
(a.) After compilation of program files, following error messages were shown on
command prompt. Identify the causes for those specific errors. [4 Marks]
i.) Testing.java:8:package system does not exist.
system.out.println(“No of items:”+totalItem);
^
1 error
ii.) Testing.java:10: cannot find symbol
Location: class Testing
Symbol: variable count
++count;
^
2 errors
(b.) Find the error in each of the following code segment and explain how to
correct it. [4 Marks]
i.) For(index =0.1;index!=1.0;index+=0.1)
System.out.println(“Index =” + index);
ii.) Switch(x)
{
case 1:
System.out.println(“The number is 1”);
Break;
case 2:
System.out.println(“The number is 2”);
Default:
System.out.println(“The number is not 1 or 2”);
Break;
}
(c.) Compare and contrast the while and for loop repetition statement [2 Marks]
(d.) Write the output if the following code is executed. [4 Marks]
class LabelTest
{
public static void main (String arg[])
{
xyz:
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 2; j++) {
System.out.println("i is " + i + ", j is " + j);
if (( i + j) > 3)
break xyz;
}
System.out.println("End of loops");
}
}
(e.) What would be the output if the following code is executed ? [6 Marks]
class PatPrint
{
public static void main(String[] args)
{
for(int i=0;i<2;i++)
{
for(int j=0; j<1;j++)
System.out.print("*");
System.out.print("\n");
for(int j=0; j<2;j++)
System.out.print("*");
System.out.print("\n");
for(int j=0; j<3;j++)
System.out.print("*");
System.out.print("\n");
for(int j=0; j<4;j++)
System.out.print("*");
System.out.print("\n");
for(int j=0; j<5;j++)
System.out.print("*");
System.out.print("\n");
}
}
}
4
(a.) Fill in the blanks in each of the following statements. [4 Marks]
i.) The ………………..statement in a called method can be used to pass the value of an expression back to the calling method.
ii.) An array that uses two indices is referred to as a(n)…………array.
iii.) The name of the element in row 3 and column 5 of array d is ………..
iv.) Command-line arguments are stored in …………………………..
(b.) Determine whether each of the following is true or false. [4 Marks]
i.) An array can store many different types of values.
ii.) An array index should normally be of type double.
iii.) Command-line arguments are separated by white space.
iv.) Expression array.length is used to access the number of arguments of a variable-length argument called array.
(c.) Perform the following tasks for an array called table: [3 Marks]
i.) Declare and create the array as an integer array has three rows and three columns. Assume that the constant ARRAY_SIZE has been declared to be 3.
ii.) How many elements does the array contain?
iii.) Use a for statements to initialize each element of the array to the sum of its indices. Assume that the integer variable x and y are declared as control variable.
(d.) Write the output of the following program if it is executed. [4 Marks]
class FamilyList
{
String[] personNames = {"Mendis", "Ravi", "Nihar", "James"};
String[] personWifeNames = new String[personNames.length];
void printPersonNames()
{
for(int i = 0; i< personNames.length; i++)
{
System.out.println(personNames[i]);
}
}
void printPersonWifeNames()
{
for(int i = 0; i< personWifeNames.length; i++)
{
System.out.println(personWifeNames[i]+" "+ personNames[i]);
}
}
public static void main (String args[])
{
FamilyList familyList = new FamilyList();
System.out.println("List of Male");
System.out.println("-------------");
familyList.printPersonNames();
familyList.personWifeNames[0]= "Malathy";
familyList.personWifeNames[1]= "Priya";
familyList.personWifeNames[2]= "Susmina";
familyList.personWifeNames[3]= "Rosemary";
System.out.println();
System.out.println("List of Female");
System.out.println("---------------");
familyList.printPersonWifeNames();
}
}
(e.) Write method distance to calculate the distance between two points
(x1, y1) and (x2, y2). All numbers and return values should be of type double. Incorporate this method into an application that enables the user to enter the coordinates of the points. [5 Marks]
5.
(a.) Complete the following statements with suitable terms. [5 Marks]
i.) The ………… method is called by the garbage collector just before it reclaims an object’s memory.
ii.) Keyword …………specifies that a variable is not modifiable.
iii.) A(n)……………….consists of a data representation and the operations that can be performed on the data.
iv.) ……………………is a form of software reusability in which new classes acquire the member of existing classes and embellish those classes with new capabilities
v.) A superclass’s …………members are accessible anywhere that the program has a reference to an object of that superclass or to an object of one of its subclass.
(b.) Define what the following terms means: [6 Marks]
i.) abstract class
ii.) concrete class
iii.) Interface
(c.) What will be the output if the following code is executed? [6 Marks]
class VariableScopeTest
{
static int num1 = 10;
int num2 = 15;
void printNumGroup1()
{
System.out.println("Number2 = " + num2);
}
public static void main (String args[])
{
System.out.println("Number1 = " + num1);
VariableScopeTest vST = new VariableScopeTest();
vST.printNumGroup1();
vST.printNumGroup2();
System.out.println("Number2 = " + vST.num2);
}
void printNumGroup2()
{
int num2 = 20;
7
System.out.println("Number2 = " + num2);
System.out.println("Number2 = " + this.num2);
}
}
(d.) Define class variable, instance variable and local variable. Give an example
from the source code given (c.). [3 Marks]
6.
(a.) What will be the output if the following code is executed? [5 Marks]
public class TestMathFun
{
public static void main(String [] args)
{
double val1 = 9.2;
double val2 = -23.3;
double val3 = -9.8;
System.out.printf("%s\t%s\t%s\t%s\t\n","value","abs","ceil","floor");
System.out.printf( "%.1f\t%.1f\t%.1f\t%.1f\t\n", val1,Math.abs(val1),Math.ceil(val1),Math.floor(val1));
System.out.printf( "%.1f\t%.1f\t%.1f\t%.1f\t\n", val2,Math.abs(val2),Math.ceil(val2),Math.floor(val2));
System.out.printf( "%.1f\t%.1f\t%.1f\t%.1f\t\n", val3,Math.abs(val3),Math.ceil(val3),Math.floor(val3));
}
}
(b.) List five common examples of exceptions. [2.5 Marks]
(c.) Give reasons why exception-handling techniques should not be used for
conventional program control. [2 Marks]
(d.) If no exceptions are thrown in try block, where does control proceed to when the
try block completes execution? [1.5 Marks]
(e.) What would be the output of the following source code if an integer numerator is
divided by a zero denominator? [5 Marks]
import java.util.Scanner;
public class TryCatchTest
{
public static int quotient( int numerator, int denominator )
8
{
return numerator / denominator;
}
public static void main( String args[] )
{
Scanner scanner = new Scanner( System.in );
boolean continueLoop = true;
do
{
try
{
System.out.print( "Enter an integer for numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Enter an integer for denominator: " );
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
System.out.printf( "\nResult: %d / %d = %d\n", numerator, denominator, result );
continueLoop = false;
}
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println( "Zero is an invalid denominator. Please try again.\n" );
}
}while(continueLoop);
}
}
(f.) Define each of the following terms. [4 Marks]
i.) thread
ii.) multithreading
iii.) runnable state
iv.) timed waiting state