adhitz

counter

hit counter

on

adhitz

Sunday, May 8, 2011

all the question and answer for java to all NCC education students

QUESTION 1
Marks
Write four different Java statements where each statement adds 1 to integer variable x. 4
Answer x =x+1;
x+=1;
++x;
x++;
1
1
1
1
QUESTION 2
Write Java statements to accomplish each of the following tasks:
a) Assign the sum of x and y to z and increment the value of x by 1 after the calculation. 2
Answer z=x++ + y; (accept alternative multiline statements)
b) Test if the value of the variable count is greater than 10. If it is, print “Count is greater than 10”. 2
Answer if(count > 10)
System.out.println(“Count is greater than 10”);
QUESTION 3
Name FOUR (4) primitive types of data provided by the Java programming language. 4
Answer -int -char
-float -long
-byte -double
-boolean -short
[1 mark each, max 4 marks]
QUESTION 4
Write the escape sequence for each of the following in the Java programming language:
a) Backspace
b) Tab
c) Newline or linefeed
d) Form feed
4
Answer (a) \b (b) \t (c) \n (d) \f
[1 mark each, max 4 marks]
Java September 2009 Final © NCC Education Ltd 2009
QUESTION 5
Show the value of x after each of the following statements is performed:
a) x=Math.ceil(9.2);
b) x=Math.floor(-9.8);
c) x=Math.pow(9, .5);
d) x=Math.abs(-7.5);
1
1
1
1
Answer a) 10
b) -10
c) 3
d) 7.5
QUESTION 6
Assume i=1, j=2, k=3, and m=2. What does each of the following statements print?
a) System.out.println( i >=1 && j<4);
b) System.out.println( !(k>m));
c) System.out.println(m<=99 && k<m);
d) System.out.println(j>=i || k==m);
1
1
1
1
Answer a) true
b) false
c) false
d) true
QUESTION 7
Write a catch block to demonstrate exception handling when an array subscript is not within the
allowed range.
catch(IndexOutofBoundsException error)
{
System.out.println(“Index error”);
}
3
1
Java September 2009 Final © NCC Education Ltd 2009
QUESTION 8
Given String myName = “STACY”, write the method to perform the following tasks.
a) Convert myName to lower case. 2
myName.toLowerCase();
b) Test if myName ends with “Y”; 2
myName.endsWith(“Y”);
QUESTION 9
a) Differentiate between an interface and a class. 2
An interface looks much like a class, except all of its methods must be abstract and all of its
data (if any) must be static final.
b) Create an interface called Working with a single method named work(). 2
public interface Working (1 mark)
{
public void work(); (1 mark)
};
QUESTION 10
Write a Java statement to create each of the following layouts:
a) Creates a layout with two rows, three columns, a horizontal space of two pixels, and a vertical
space of four pixels.
1
GridLayout gr = new GridLayout(2,3,2,4);
b) Creates a card layout with a horizontal gap of 5 pixels and a vertical gap of 10 pixels. 1
CardLayout cl = new CardLayout(5,10);
c) Creates a flow layout with right align. 1
FlowLayout f = new FlowLayout(FlowLayout.RIGHT);
d) Creates a border layout with an east region. 1
BorderLayout b = new BorderLayout(BorderLayout.EAST);
Total 40 Marks
Java September 2009 Final © NCC Education Ltd 2009
SECTION B
ANSWER ANY TWO QUESTIONS IN THIS SECTION
QUESTION 1
Throughout the question, please credit any valid alternative point.
Marks
a) Write a method isEven which uses the modulus operator to determine if an integer is even.
The method should take an integer argument and return true if the integer is even and false
otherwise.
6
boolean isEven(int num)
{
if(num%2 ==0)
return true;
return false;
}
2
21
1
b) i) Define the term overloading. 2
Overloading involves one term to indicate diverse meanings, or writing multiple
methods with the same name, but with different arguments.
ii) Explain why the following two methods cannot coexist within a program:
int aMethod(int x);
void aMethod(int x);
2
The compiler determines which of several versions of a method to call based on
argument lists. When the method call aMethod(17); is made, the compiler will not
know which method to execute because both methods take an integer argument.
Java September 2009 Final © NCC Education Ltd 2009
iii) Demonstrate function overloading by declaring two functions to calculate and display
the area of a square and rectangle individually.
6
public static void area(int x)
{ int a;
a=x*x;
system.out.println{“area of square is “ +a};
}
public static void area(int x, int y)
{ int a;
a=x*y;
system.out.println{“Area of rectangle is “ +a};
}
3
3
Java September 2009 Final © NCC Education Ltd 2009
c) Rewrite the following program segment using the switch structure.
if(year ==1)
System.out.println(“Freshman”);
else if(year ==2)
System.out.println(“Sophomore”);
else if(year ==3)
System.out.println(“Junior”);
else if(year ==4)
System.out.println(“Senior”);
else System.out.println(“Invalid Year”);
5
switch(year)
{
case 1:
System.out.println(“Freshman”);
break;
case 2:
System.out.println(“Sophomore”);
break;
case 3:
System.out.println(“Junior”);
break;
case 4:
System.out.println(“Senior”);
break;
default:
System.out.println(“Invalid year”);
}
1
3
1
d) Write a program that shows the sum of the integers 1 to n for every n from 1 to 50. That is,
the program prints 1, 3 (the sum of 1 and 2), 6 (the sum of 1, 2 and 3), and so on.
6
public class sum{
public static void main(String args[])
{
int sum=0;
for(int i=1; i<=50; i++)
{
sum+=i;
System.out.print(“ “ + sum);
}
}
1
1
1
2
1
Java September 2009 Final © NCC Education Ltd 2009
e) A monthly car insurance premium is $200 if the driver is aged 25 or younger, and $125 if the
driver is aged 26 or older. Write a program using the conditional operator for the above
statement.
3
public class conditionalOperator{
public static void main(String args[])
{
int premium, age;
premium =(age<=25) ? 200: 125;
}
3
Total 30 Marks
Java September 2009 Final © NCC Education Ltd 2009
QUESTION 2
Throughout the question, please credit any valid alternative point.
Marks
a) i) Create a class Employee with data fields to hold the employee number and employee
salary (empNum and empSal), a constructor and a get method for each field.
6
public class Employee
{
private int empNum;
private double empSal;
Employee(int e, double s)
{
empNum = e;
empSal = s;
}
public int getEmpNum()
{
return empNum;
}
public double getEmpSal()
{
return empSal;
}
};
NB: accept empSal as an integer in which case the get method should return an
integer.
2
2
1
1
ii) Declare an array named emp that holds seven Employees. 2
Employee [] emp = new Employee[7];
iii) Assume that you want to number your Employees 101, 102, 103, and so on, and start
each Employee at a salary of $500.00, construct seven Employee objects and add them
to the array emp.
3
for(int x=0; x<7; x++)
emp[x] = new Employee(101 +x, 500.00);
iv) Print the data for seven Employees stored in the emp array. 3
for (int x=0; x<7; x++)
System.out.println(emp[x].getEmpNum() + “ “ +
emp[x].getEmpSal());
Java September 2009 Final © NCC Education Ltd 2009
b) i) What is an abstract class? 2
An abstract class is one from which you cannot create any concrete objects, but from
which you can inherit.
ii) What is an abstract method? 4
An abstract method is a method with no method statements.
When you create an abstract method, you provide the keyword abstract and the
intended method type, name, and arguments, but you do not provide any statements
within the method.
22
iii) Discuss the similarity and difference between abstract classes and interfaces. 4
Abstract classes and interfaces are similar in that you cannot instantiate concrete
objects from either. (2 marks)
Abstract classes differ from interfaces because abstract classes can contain
nonabstract methods, but all methods within an interface must be abstract. (2 marks)
iv) Create an abstract Animal class containing a data field for the name, a constructor, a
getName() method, and an abstract speak() method.
6
public abstract class Animal
{
private String name;
public Animal(string nm)
{
name = nm;
}
public String getName()
{
return name;
}
public abstract void speak();
}
1
11
1
2
Total 30 Marks
Java September 2009 Final © NCC Education Ltd 2009
QUESTION 3
Throughout the question, please credit any valid alternative point.
Marks
a) i) Write a class named Staff that has THREE (3) attributes for name, overtime hours and
overtime rate. In class Staff, you should write at least TWO (2) constructors to
initialize the variables, one to default and one to initialize based on constructor
arguments. You should also write a method called getOvertimeClaim() that returns
overtime hours multiplied by overtime rate.
7
ii) Write another class called Contract derived from the Staff class. The Contract class
has TWO (2) constructors that call the parent constructor directly. A Contract has
overtime of EIGHT (8) hours with the overtime rate of 10 by default.
3
iii) Write a main() method to test the Contract class. 3
i) public class Staff {
private String name;
private double overtimeHours;
private double overtimeRate;
public Staff() {
name = "";
overtimeHours = 0.0;
overtimeRate = 0.0;
}
public Staff(String n, double h, double r) {
name = n;
overtimeHours = h;
overtimeRate = r;
}
public double getOvertimeClaim() {
return overtimeHours * overtimeRate;
}
}
ii) public class Contract extends Staff {
public Contract () {
super();
}
public Contract (String n) {
super(n, 8,10);
}
iii) public static void main(String args[]) {
Contract c = new Contract ("John");
System.out.println("Overtime Claim is " +
c.getOvertimeClaim());
}
}
1
2
2
2
1
1
1
1
1
1
Java September 2009 Final © NCC Education Ltd 2009
b) Given the statement:
String letters = “abcdefghijklmabcdefghijklm”
What would be assigned to the variables if the following statements are executed:
i) int val = letters.indexOf( ‘c’ ); 2
2
ii) int val = letters.indexOf( ‘a’,1); 2
13
iii) int val = letters.indexOf( ‘$’); 2
-1
iv) int val = letters.lastIndexOf( ‘

0 comments:

Post a Comment

Popular Posts

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Justin Bieber, Gold Price in India