0% found this document useful (0 votes)
4 views3 pages

Java Assignment Questions

The document outlines a Java assignment focused on implementing an Animal class with attributes and methods, along with examples of class and object concepts. It also covers Java data types, their default values, and variable scopes with explanations and code examples. Students are instructed to complete the assignment in pairs and submit a printout after execution.

Uploaded by

Anilnailk
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)
4 views3 pages

Java Assignment Questions

The document outlines a Java assignment focused on implementing an Animal class with attributes and methods, along with examples of class and object concepts. It also covers Java data types, their default values, and variable scopes with explanations and code examples. Students are instructed to complete the assignment in pairs and submit a printout after execution.

Uploaded by

Anilnailk
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/ 3

Java Assignment

1) Animal Class Implementation in Java


Develop an Animal class with attributes species and sound. Include methods to get and set
these attributes.
Instantiate an Animal object and show how to access its members.

Example Code:

public class Animal {


private String species;
private String sound;

// Constructor
public Animal(String species, String sound) {
this.species = species;
this.sound = sound;
}

// Getter and Setter for species


public String getSpecies() {
return species;
}

public void setSpecies(String species) {


this.species = species;
}

// Getter and Setter for sound


public String getSound() {
return sound;
}

public void setSound(String sound) {


this.sound = sound;
}

// Display method
public void display() {
System.out.println("Species: " + species + ", Sound: " + sound);
}
public static void main(String[] args) {
Animal a = new Animal("Dog", "Bark");
a.display();
a.setSound("Woof");
a.display();
}
}

Assignment Questions

1. Explain the concept of a class and an object in Java with a real-world analogy.
A class is a blueprint or template for creating objects. It defines properties (attributes) and
behaviors (methods).
For example, consider a class 'Car'. It defines attributes like color, brand, speed and
methods like drive(), brake().

An object is an instance of a class. If 'Car' is the class, then 'myCar = new Car()' is the object,
representing a specific car with actual values.

Analogy: Class is like a recipe, object is the actual dish cooked using that recipe.

2. What are the different categories of data types in Java, and how do they
differ?
Java data types are divided into:
- Primitive Data Types: byte, short, int, long, float, double, char, boolean. These hold simple
values.
- Non-Primitive (Reference) Data Types: Strings, Arrays, Classes, Interfaces. These hold
references to objects.

Primitive types are predefined and have fixed size, while non-primitive types are created by
the programmer and can have methods.

4. What is the default value assigned to different primitive data types in Java if
not explicitly initialized?
- byte, short, int, long: 0
- float, double: 0.0
- char: '\u0000' (null character)
- boolean: false
- Object references: null
5. What are the different types of variable scopes in Java? Explain each with an
example.
Java has the following variable scopes:
- Local Variables: Declared inside a method. Only accessible within the method.
- Instance Variables: Declared inside a class but outside any method. Each object has its own
copy.
- Static Variables: Declared with static keyword. Shared across all instances.

Example:

public class ScopeExample {


int instanceVar = 10; // Instance variable
static int staticVar = 20; // Static variable

public void method() {


int localVar = 30; // Local variable
System.out.println(localVar);
}
}

All are instructed to implement the programs given below against your roll numbers in
team of two, and submit the same with printout copy after completion of execution.

You might also like