Java Assignment Sem - 1 Mca
Java Assignment Sem - 1 Mca
Theory Questions
1. Explain the different primitive data types available in Java and their respective sizes.
How do Java’s data types differ from those in other programming languages like C++
or Python?
->
Primitive Data Types in Java and Their Sizes
Java provides eight primitive data types, which are predefined by the language and serve as
the building blocks for data manipulation. These data types, along with their sizes and default
values, are:
Data
Size (in bytes) Default Value Range
Type
-9,223,372,036,854,775,808 to
long 8 0L
9,223,372,036,854,775,807 (-2^63 to 2^63 - 1)
'\u0000' (null
char 2 0 to 65,535 (Stores a single Unicode character)
character)
Java’s data types have several differences compared to C++ and Python:
IEEE 754 (float = 4 IEEE 754 (float, double, Uses float (≈ 8 bytes,
Floating-Point
bytes, double = 8 and long double with IEEE 754 double
Precision
bytes) varying precision) equivalent)
Key Takeaways
Java has fixed primitive data type sizes, unlike C++, where sizes depend on the
architecture.
Java is strictly type-safe, preventing implicit type conversions that C++ allows.
Python does not have primitive data types, instead, all data types are objects, leading
to more flexibility but higher memory usage.
Boolean values in Java and Python are strict, whereas C++ treats 0 as false and any
nonzero value as true.
2. What is the difference between a class and an object in Java? Describe how to define
a class and create an object from it. Provide an example of a simple class definition and
object instantiation.
->
1. Class
2. Object
Each object has its own copy of instance variables and can invoke methods defined in
the class.
To define a class, use the class keyword followed by the class name. It typically contains:
class Car {
String brand;
int speed;
this.speed = speed;
void displayInfo() {
myCar.displayInfo();
Output:
->
Control flow statements in Java determine the execution order of statements in a program.
These include decision-making statements (if, if-else, switch) that allow the program to
execute specific code blocks based on conditions.
1. if Statement
Example: Using if
}
}
Output:
2. if-else Statement
The if-else statement provides an alternative block of code that runs when the condition is
false.
} else {
System.out.println("Number is 5 or less");
Output:
Number is 5 or less
int num = 0;
if (num > 0) {
System.out.println("Positive number");
System.out.println("Negative number");
} else {
System.out.println("Zero");
}
}
Output:
Zero
4. switch Statement
The switch statement allows selecting a block of code to execute from multiple options based
on a variable's value.
Syntax:
switch(expression) {
case value1:
// Code block
break;
case value2:
// Code block
break;
default:
}
Each case represents a possible value of the variable.
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Invalid day");
Output:
Wednesday
Key Differences Between if-else and switch
Supports relational (>, <, ==, Works with int, char, String, enum, byte,
Data types
etc.) short
->
Loops in Java
Loops in Java repeat a block of code until a specific condition is met. They help automate
repetitive tasks and reduce code duplication.
2. while loop – Best when the number of iterations is unknown (runs as long as a
condition is true).
3. do-while loop – Similar to while, but executes the loop at least once before checking
the condition.
1. for Loop
Syntax:
} }}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
2. while Loop
Syntax:
while(condition) {
int i = 1;
while (i <= 5) { // Condition is checked before execution
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
3. do-while Loop
Executes the loop at least once, even if the condition is false initially.
Syntax:
do {
} while(condition);
int i = 1;
do {
System.out.println("Number: " + i);
i++; // Increment
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Key Differences Between for, while, and do-while
->
Purpose
In Java, getter and setter methods are used to access and modify private fields in a class. They
help encapsulate data by restricting direct access and allowing controlled modification.
o Named as getVariableName().
o Named as setVariableName(value).
class Person {
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
return name;
return age;
if (age > 0) {
this.age = age;
} else {
System.out.println("Invalid age");
}
}
public class Main {
// Creating an object
// Using getters
// Using setters
person.setName("Bob");
person.setAge(30);
Expected Output
Name: Alice
Age: 25
Invalid age
When to Use?
Code :
// Integer type
int a = 10, b = 5;
// Floating-point type
// Character type
// Boolean type
// Output results
System.out.println("Integer Operations:");
System.out.println("\nFloating-point Operations:");
System.out.println("Division of " + x + " by " + y + " = " + division);
System.out.println("\nCharacter Operations:");
System.out.println("\nBoolean Values:");
Output :
+
2. Create a Java class named Person with private fields for name and age. Implement
getter and setter methods for these fields. Write a main method to create an instance of
Person, set its properties using the setter methods, and retrieve them using the getter
methods.
Code :
// Person.java
// Private fields
return name;
this.name = name;
}
return age;
this.age = age;
} else {
person.setName("John Doe");
person.setAge(25);
System.out.println("Person Details:");
OUTPUT :
3. Develop a Java program that uses control flow statements. Include examples of if-else
and switch statements. For instance, create a program that takes a numeric grade as
input and prints the corresponding letter grade using a switch statement.
Code :
// GradeEvaluator.java
import java.util.Scanner;
} else {
String letterGrade;
case 9:
letterGrade = "A";
break;
case 8:
letterGrade = "B";
break;
case 7:
letterGrade = "C";
break;
case 6:
letterGrade = "D";
break;
default:
letterGrade = "F";
break;
scanner.close();
}
Output :
4. Write a Java program that demonstrates the use of different types of loops. Create a
for loop to print numbers from 1 to 10, a while loop to print even numbers from 2 to 20,
and a do-while loop to print numbers from 10 to 1 in descending order.
Code :
// LoopDemo.java
System.out.println("\n");
int num = 2;
while (num <= 20) {
num += 2;
System.out.println("\n");
do {
count--;
System.out.println();
}
Output :
5. Create a Java class with a method that overrides a method from its superclass. Define
a superclass named Animal with a method makeSound(). Create a subclass named Dog
that overrides the makeSound() method. In the main method, instantiate a Dog object
and call the makeSound() method.
Code :
// MethodOverriding.java
class Animal {
// Superclass method
}
// Subclass Dog that overrides makeSound()
@Override
// Main class
myDog.makeSound();
}
Output :