You are on page 1of 6

1) Programming Question:

Design a class named Student to represent a student information. The


class contains:
• A string data field named studName that specifies the student name.
• An integer data field named studAge that specifies the student age.
• A double data field named mark that specifies the student mark.
 A method named status which returns “Pass” if the mark is greater
than or equal 60, otherwise it returns “Fail”

Create an object of the previous class Student and print student


information, and call the method status to print weather the student is
passes or failed.
2) Questions:
1. Consider the following class:
public class IdentifyMyParts {
public int x = 7;
public int y = 3;
}
a) What are the instance variables?
b) What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
2. What's wrong with the following program?
public class SomethingIsWrong {
public static void main(String[] args) {
Rectangle myRect;
myRect.width = 40;
myRect.height = 50;
System.out.println("myRect's area is " + myRect.area());
}
}

3. Given the following class, called NumberHolder, write some code


that creates an instance of the class, initializes its two member
variables, and then displays the value of each member variable.
public class NumberHolder {
public int anInt;
public float aFloat;
}
4. Predict the output of following Java program?
class Test {
int i;
}
public class TestClass {
public static void main(String[] args) {
Test t;
System.out.println(t.i);
}
}
1) Write an output of the following class:
public class Rectangle {
double width, length;
double Area ( ){
double a= width*length;
return a;
}
double perimeter( ){
return 2* width + 2*length;
}
public static void main(String [ ] aregs){
Rectangle r1= new Rectangle ();
r1.width=2;
r1.length=3;
System.out.println("r1 Area = "+r1.Area());
System.out.println("r1 Perimeter = "+r1.perimeter());
Rectangle r2 = new Rectangle();
r2.width=5;
r2.length=4;
System.out.println("Width of r2 = "+r2.width);
System.out.println("Length of r2 = "+r2.length);
System.out.println("r2 Area = "+r2.Area());
System.out.println("r2 Perimeter = "+r2.perimeter());
}
}
2) Analyze the following code:
public class Test {
public static void main(String[] args) {
double radius;
final double PI= 1.0;
double area = radius * radius * PI;
System.out.println("Area is " + area);
}
}
a. The program has compile errors because the variable radius is not
initialized.
b. The program has a compile error because a constant PI is defined inside a
method.
c. The program has no compile errors but will get a runtime error because
radius is not initialized.
d. The program compiles and runs fine.

You might also like