Java ASSIGNMENT 1 Key (Based on Provided Notes - Exam Format)
1. Explain the key features of the Java Programming language.
Based on the provided notes, the key features of Java include:
● Object Oriented: Java implements fundamental Object-Oriented Programming
System (OOPS) concepts such as Object, Class, Inheritance, Polymorphism,
Abstraction, and Encapsulation. In Java, everything is treated as an object.
● Platform Independent: Java code is compiled into platform-independent
bytecode. This bytecode can be executed on any machine with a Java Virtual
Machine (JVM), embodying the "Write Once, Run Anywhere" principle.
● Simple: Java's syntax is based on C/C++, making it relatively easier to learn for
those familiar with these languages, especially if OOPS concepts are understood.
● Secure: Java facilitates the development of secure systems (virus-free,
tamper-free) through features like bytecode verification and security managers.
● Architecture-neutral: The Java compiler produces an architecture-neutral
object file format (bytecode), enabling execution on various processors equipped
with a Java runtime system.
● Portable: Portability arises from Java's architecture-neutral nature and lack of
implementation-dependent aspects.
● Robust: Java emphasizes reliability by aiming to eliminate errors through
compile-time and runtime checking mechanisms.
● Multithreaded: Java inherently supports multithreading, allowing programs to
perform multiple tasks concurrently, which is beneficial for interactive
applications.
● Interpreted: Java bytecode is interpreted (translated on the fly) into native
machine instructions by the JVM during execution.
● High Performance: Performance is enhanced through the use of Just-In-Time
(JIT) compilers within the JVM, which compile bytecode to native code at runtime.
● Distributed: Java was designed with distributed environments, like the internet,
in mind.
● Dynamic: Java is dynamic, capable of adapting to evolving environments and
carrying runtime type information for verifying object access.
2. Differentiate between JDK, JRE, and JVM. Define variables and data types in
Java. Differentiate between primitive data types reference data types.
● JDK, JRE, and JVM Differentiation:
○ JVM (Java Virtual Machine): As per the notes, the JVM is an abstract
machine that provides the runtime environment for executing Java bytecode.
It handles loading, verifying, and executing code. It interprets bytecode for
specific platforms.
○ JRE (Java Runtime Environment): The notes describe the JRE as the
implementation of the JVM. It constitutes Java's runtime environment, making
Java a platform.
○ JDK (Java Development Kit): The notes state that the JDK must be installed
to write Java programs, implying it contains the necessary development tools
(like the compiler) required in addition to the JRE.
○ In summary: JDK is for development (includes JRE + development tools), JRE
is for running Java applications (includes JVM + libraries), and JVM is the core
virtual machine that executes the bytecode.
● Variables: A variable, according to the notes, is a "name of reserved area
allocated in memory".
○ Example: int i = 50;
● Data Types: Data types define the kind of values a variable can hold. The notes
classify them into two categories:
○ Primitive Data Types: These are fundamental types predefined by Java. The
eight primitive types listed are byte, short, int, long, float, double, boolean,
and char.
○ Non-Primitive Data Types (Reference Types): These refer to objects.
Examples provided include String and Array.
● Primitive vs. Reference Data Types Differentiation: The primary distinction
(though not explicitly detailed in the provided snippets) is that primitive types
store the actual value directly in memory, while reference types store a memory
address (reference) pointing to where the actual object data is located.
3. Differentiate between C++ and JAVA.
Based on the comparisons available in the notes:
● Java is platform-independent (uses bytecode and JVM), whereas C++ typically
compiles to platform-specific machine code.
● Java's syntax is derived from C/C++, making it relatively simple for programmers
with a C/C++ background.
● Java is described as more dynamic than C++, designed to adapt to changing
environments.
● (Further comparisons regarding pointers, memory management etc., are not
detailed in the provided notes.)
4. What is JAVA and Internet?
The notes state that Java was "designed for the distributed environment of the
internet". Its platform independence allows Java bytecode to be easily distributed and
executed across the web on any machine running a JVM.
5. Explain JAVA support systems and JAVA environment?
The Java support systems and environment, as indicated by the notes, comprise:
● JVM: Executes the bytecode.
● JRE: Provides the runtime environment (JVM implementation + libraries) needed
to run Java applications.
● JDK: Provides the tools (compiler, etc.) needed to develop Java applications
(includes JRE).
● Environment Setup: Configuring the system PATH to locate Java executables is
also part of the environment setup.
6. Define JAVA program structure?
A basic Java program consists of a class containing the main method, which is the
entry point for execution.
● Basic Structure:
class ClassName {
public static void main(String args[]) {
// Program statements
[Link]("...");
}
}
● General Class Structure: A class serves as a blueprint and can contain:
class classname {
// Instance variables (state/data)
type instanceVariable1;
// ...
// Methods (behavior/code)
type methodName1(parameter-list) {
// Method body
}
// ...
}
7. Brief explanation of Tokens?
Tokens are the smallest individual units or building blocks of a Java program
recognized by the compiler. The notes list five types:
1. Reserved Keywords: Words with predefined meanings (e.g., class, public, int, if).
2. Identifiers: Names given to classes, methods, variables, etc.
3. Literals: Constant values represented directly (e.g., 100, 3.14f, 'A', "Hello").
4. Operators: Symbols performing operations (e.g., +, -, *, /, =).
5. Separators: Symbols used for structure and separation (e.g., ;, {}, (), ,, .).
8. What is Statements in JAVA?
While not formally defined in the notes, Java statements are inferred from examples to
be complete units of execution. They typically correspond to a single logical step in
the program and often end with a semicolon (;). Examples include variable
declarations, assignments, method calls, and control flow structures.
9. Define Constant and Variables with example?
● Variable: A variable is a named memory location used to store data. Its value can
usually be changed during program execution.
○ Example: int age = 25;
● Constant: The notes do not explicitly define or demonstrate constants. In Java,
constants are typically variables whose values cannot be changed after
initialization, declared using the final keyword. Although final is listed as a
keyword/modifier in the notes, its use for defining constants is not elaborated
upon.
10. Define Data Types with example?
Data types specify the type of data a variable can hold and the operations allowed.
The two main categories in Java, according to the notes, are:
● Primitive Data Types: Basic types built into the language.
○ Examples: int count = 10;, float price = 99.95f;, boolean isActive = true;, char
initial = 'J';
● Non-Primitive Data Types (Reference Types): Refer to objects created from
classes.
○ Examples: String message = "Welcome";, Array (e.g., int[] numbers = new
int[5];)
11. Write Declaration of variables and Scope of variables?
● Declaration of Variables: Declaring a variable involves specifying its data type
followed by its identifier (name). Initialization (assigning an initial value) can be
done simultaneously.
○ Syntax: dataType variableName; or dataType variableName = initialValue;
○ Examples: int count;, String name = "Java";
● Scope of Variables: The scope determines the region of the program where a
variable is accessible. The provided notes do not explicitly discuss the different
scopes of variables (e.g., local, instance, class/static).
12. Write a Symbolic constants with examples?
Symbolic constants (named constants, typically declared using final in Java for
readability and maintainability) are not explicitly defined or demonstrated with
examples in the provided notes.
13. Define Type Casting in brief with program?
Type casting is the process of converting a value from one data type to another. The
notes illustrate one type:
● Widening Casting (Implicit): This is an automatic conversion performed by Java
when converting a value from a smaller data type to a compatible larger data type
(e.g., int to float). No data loss occurs.
○ Example Snippet (based on notes):
int numInt = 10;
float numFloat = numInt; // Widening cast from int to float
[Link](numInt); // Output: 10
[Link](numFloat); // Output: 10.0
● Narrowing Casting (Explicit): Converting from a larger data type to a smaller
one requires an explicit cast using the (targetType) operator and may result in
data loss. This type is not covered in the provided notes.
14. Define all Java operators with program?
Operators are symbols used to perform operations on variables and values. They are
listed as a type of token in the notes. Examples like assignment (=), addition (+), and
object creation (new) are used.
However, a comprehensive definition, categorization (e.g., Arithmetic, Relational,
Logical, Bitwise, Assignment, etc.), and an example program demonstrating all
operators are not available in the provided sections of the notes.
15. Explain Special, Expressions and its evaluation
● Special Operators: The notes do not specifically categorize any operators as
"special". Symbols like (), {}, [], ;, ,, . are listed as "Separators" under tokens.
● Expressions: An expression is a combination of variables, operators, literals, and
method calls that evaluates to a single value. The notes use expressions (e.g., i + j)
but do not formally define the term.
● Evaluation: The rules governing the order in which operations within an
expression are performed (operator precedence and associativity) are not
discussed in the provided notes.
16. Defining a Class, Adding variables and Methods to classes
● Defining a Class: A class is defined using the class keyword. It serves as a
blueprint for creating objects.
○ Syntax: class ClassName { /* class body */ }
● Adding Variables (Instance Variables): These represent the state or data of an
object and are declared within the class body.
○ Syntax: dataType variableName;
● Adding Methods: These define the behavior or actions an object can perform
and are defined within the class body.
○ Syntax: returnType methodName(parameterList) { /* method body */ }
● Conceptual Example:
class Box {
double width; // Instance variable
double height; // Instance variable
void calculateVolume() { // Method
// ... method body ...
}
}
17. How to Creating Objects explain with example
Objects are instances created from a class definition. The new keyword is used to
allocate memory for the object and invoke its constructor to initialize it.
● Syntax: ClassName objectReference = new ClassName(arguments);
● Example (using the conceptual Box class):
Box myBox = new Box(); // Creates an object of the Box class
18. Accessing Class Members.
Accessing the instance variables and methods of an object is typically done using the
object reference followed by the dot (.) operator. The provided introductory sections
of the notes do not explicitly detail this mechanism, although method invocation is
mentioned generally.
19. Define Constructors and write a one program of constructor.
● Definition: A constructor is a special block of code within a class used to initialize
objects of that class when they are created.
○ It must have the exact same name as the class.
○ It does not have an explicit return type (not even void).
○ It is called automatically when an object is created using the new keyword.
○ According to the notes, it cannot be abstract, static, final, or synchronized.
● Example Program (Parameterized Constructor):
// File: [Link]
class Book {
String title;
String author;
int yearPublished;
// Parameterized Constructor
Book(String bookTitle, String bookAuthor, int pubYear) {
[Link]("Creating a new Book object...");
title = bookTitle;
author = bookAuthor;
yearPublished = pubYear;
}
// Method to display book details
void displayDetails() {
[Link]("Title: " + title);
[Link]("Author: " + author);
[Link]("Year Published: " + yearPublished);
}
public static void main(String args[]) {
// Creating Book objects using the constructor
Book book1 = new Book("The Hobbit", "J.R.R. Tolkien", 1937);
Book book2 = new Book("1984", "George Orwell", 1949);
[Link]("\nBook 1 Details:");
[Link]();
[Link]("\nBook 2 Details:");
[Link]();
}
}
20. Explain the types of Constructors.
The notes indicate two primary types of constructors in Java:
1. Default Constructor (No-Argument Constructor):
○ If no constructor is explicitly defined in a class, the compiler provides a
default one automatically.
○ It takes no arguments.
○ It initializes instance variables to their default values (e.g., 0 for numbers, false
for boolean, null for objects).
2. Parameterized Constructor:
○ A constructor explicitly defined by the programmer that accepts one or more
parameters.
○ Used to initialize object state with specific values passed during object
creation.
○ If any parameterized constructor is defined, the compiler does not provide a
default constructor automatically.
Additionally, the concept of Constructor Overloading allows a class to have multiple
constructors, provided each has a different parameter list (differing in the number,
type, or order of parameters). This enables object initialization in various ways.