0% found this document useful (0 votes)
2 views12 pages

JAVA lab 6

The document consists of a Java lab sheet with various questions and answers related to Java programming concepts, including constructors, object references, static methods, and class behavior. Key points include the initialization of fields, the distinction between object references and contents, and the implications of using final fields. Additionally, it discusses compile-time errors and the use of static context in methods.

Uploaded by

ytlerin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

JAVA lab 6

The document consists of a Java lab sheet with various questions and answers related to Java programming concepts, including constructors, object references, static methods, and class behavior. Key points include the initialization of fields, the distinction between object references and contents, and the implications of using final fields. Additionally, it discusses compile-time errors and the use of static context in methods.

Uploaded by

ytlerin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA LABSHEET -6

Am.sc.u4aie24029

1.Examine the following code and answer the


questions given below.

I. What is the output of line 4, why?


A: Since no values are set inside the default constructor
the roll field is initialized to 0.
II. How many objects and how many references are
created in this code? How do you find the number of
objects created? Add appropriate code for it.
A: 6 object and 7 references.
class Student {

int roll;

String name;

String address;

final int classroom = 113;

static int count = 0;

Student() {

count++;

Student(int r, String n, String a) {

this();

this.roll = r;

this.name = n;

this.address = a;

static void change(Student s) {

s.roll = 13;

s.name = "durga";

public String toString() {

return roll + " " + name;

}
III. What did you observe from output of lines 4,5,6?
Explain your answer.
A: 4TH line  0 because the default constructor doesn't set
roll
5th line  12 because roll set through constructor
6th line  after13 roll modified by the static method.

IV. What is the use of block numbered 8 ?


A: Creates an array of 3 student references. Then
initialized each element with a new student object.

V. Remove comment in line number 9 and compile? What


do you observe? Justify.
A: Compile time error. This means it's a constant and
cannot be modified after initialization. Java enforces
immutability of final fields.

VI. Comment on line 10. Comment toString() function and


run the program. What is the output?
A: Student<hash code>

2. Write down the output of the following program with


proper justification?
Class X{
static{
System.out.println(“static block”);}
}
Int x;
X(){
System.out.println(“in cons”);}
Public static void main(String args[]){
X obj;
System.out.println(“in main”);
Obj=new X();}
}

A: OUTPUT
static block runs when class is loaded before main
in main  direct statement in main()
in cons  constructor is called when object is created

3.

a. == compares object references, not contents.


s1 and s2 point to different objects in memory.
Even though the size values are the same (40), s1 !
= s2.
b. public class Shirt {
private int size;
private String brand;
private String color;
private double price;
}
c. Shirt(int size, String brand, String color, double
price) {
this.size = size;
this.brand = brand;
this.color = color;
this.price = price;
}
d. public boolean isSameShirt(Shirt other) {
return this.size == other.size &&
this.color.equals(other.color);
}
____________________________________________________________
_
4.

A:
Missing constructor argument:
Foo f = new Foo(); // ERROR
The class Foo has only one constructor:
public Foo(int x)
So this line must pass an integer.
For example:
Foo f = new Foo(10); 2
Typo in System.out.println:
System.out.prinln(a);
public class Foo {
private int a;
public Foo(int x) { a = x;
}
public void print() {
System.out.println(a);
}
}
public class FooTest {
public static void main(String[] args){
Foo f = new Foo(10);
f.print();
}
}
5.
A: The method call myMethod(); inside main will cause a
compile-time error.
Non-static method myMethod() cannot be referenced
from a static context
Make myMethod static

6.

A:
OUTPUT

7.
A:
OUTPUT

You might also like