0% found this document useful (0 votes)
150 views

Oop Assignment 1

The document contains answers to 7 questions about Java concepts. Question 1 defines bytecode, the Java Language Specification, the Java Virtual Machine, the Java Runtime Environment, the Java Development Kit, and the Just-In-Time compiler. Question 2 compares object-oriented programming and sequential programming. Question 3 explains type casting, widening, and narrowing. Question 4 lists Java's data types and operators. Question 5 defines syntax, runtime, and logic errors. Question 6 discusses why main() is declared as public and static. Question 7 discusses benefits of the object-oriented approach such as modularity, code reuse, abstraction, and encapsulation.

Uploaded by

Harshu Vagadiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Oop Assignment 1

The document contains answers to 7 questions about Java concepts. Question 1 defines bytecode, the Java Language Specification, the Java Virtual Machine, the Java Runtime Environment, the Java Development Kit, and the Just-In-Time compiler. Question 2 compares object-oriented programming and sequential programming. Question 3 explains type casting, widening, and narrowing. Question 4 lists Java's data types and operators. Question 5 defines syntax, runtime, and logic errors. Question 6 discusses why main() is declared as public and static. Question 7 discusses benefits of the object-oriented approach such as modularity, code reuse, abstraction, and encapsulation.

Uploaded by

Harshu Vagadiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment –1

Assignment -1
QUE-1: Explain role of bytecode, Java Language Specification, JVM, JRE, JDK and
JIT
ANS:
1. Bytecode: Bytecode is the intermediate representation of Java code, which is platform-
independent. It is produced by the Java compiler and interpreted by the JVM, enabling
Java’s “write once, run anywhere” capability.
2. Java Language Specification (JLS): JLS is a technical definition of the syntax and
semantics of the Java programming language. It serves as the official and authoritative
specification of the Java language.
3. Java Virtual Machine (JVM): JVM is a virtual machine that provides a runtime
environment to execute Java bytecode. It makes Java platform-independent by
converting bytecode into machine language for the underlying hardware.
4. Java Runtime Environment (JRE): JRE is a software package that contains what is
required to run a Java program, including the JVM, Java class libraries, and other
necessary components.
5. Java Development Kit (JDK): JDK is a software development environment used for
developing Java applications. It includes the JRE, an interpreter/loader (java), a compiler
(javac), an archiver (jar), a documentation generator (javadoc), and other tools needed
in Java development.
6. Just-In-Time (JIT) Compiler: The JIT compiler is a part of the JVM that improves the
performance of Java applications by compiling bytecode into native machine code at
runtime. This means that the parts of a Java program that are executed

QUE-2: Compare object-oriented programming with sequential programming


ANS:

Object-Oriented Programming Sequential Programming

Sequential programming is a list of


OOP is based on the concept of “objects”, instructions that execute one after the
which contain data and methods to other, in the order they are written. It is a
manipulate the data. This allows for a more top-down approach, starting from a main
realistic representation of real-world entities. function.
Assignment –1

In OOP, data and functions (methods) are In sequential programming, data and
bundled together as objects. This functions are separate. Functions operate
encapsulation provides a clear structure for on data, which is passed into them as
the program. arguments.

In sequential programming, code is typically


In OOP, code can be broken down into objects, organized into procedures or routines,
promoting reusability and modularity. This which can be less modular than OOP. This
makes it easier to maintain and develop can make it harder to maintain and develop
complex software systems. complex software systems.

OOP is useful for large and complex software Sequential programming can be simpler and
systems where modularity, code reuse, and more straightforward for small programs or
abstraction are important. It is also useful scripts. It is also useful when a task is
when you want to create multiple instances of naturally sequential and does not require
a class and manipulate them independently. any complex data structures or algorithms.

OOP supports inheritance, a mechanism that


allows a class to acquire properties and Sequential programming does not support
behavior of another class. This promotes code inheritance. Each function is independent
reuse and is a way to achieve runtime and does not share its code with other
polymorphism. functions.

OOP supports polymorphism, which allows a


single interface to represent different types at Sequential programming does not support
runtime. This makes the program more flexible polymorphism. Each function call is bound
and extensible. to a specific function at compile time.

OOP provides a way to abstract data and In sequential programming, data


behavior that manipulate that data into a abstraction is not inherent. However, it can
single unit called a class. This abstraction hides be achieved to some extent using structures
the complexity from the user. in C or modules in Python.

QUE-3: What is Type Casting? Explain widening and narrowing type casting.
ANS:
1. Type Casting: Type casting is a way of changing an entity from one data type to another.
It is used in programming to ensure variables are correctly processed by a function.
2. Widening Type Casting (Upcasting): Widening type casting is when a smaller primitive
data type is converted into a larger primitive data type. For example, converting an int
Assignment –1

to a long. This is done automatically by Java, as there is no loss of information in this


process.
3. Narrowing Type Casting (Downcasting): Narrowing type casting is the conversion of a
larger primitive data type to a smaller primitive data type, like converting a double to a
float. This is not done automatically by Java, as it can lead to loss of information. The
programmer must explicitly write the type in parentheses before the variable.
Here’s an example of both in Java:
// widening type casting
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0

// narrowing type casting


double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

System.out.println(myDouble); // Outputs 9.78


System.out.println(myInt); // Outputs 9
In the narrowing type casting example, the part after the decimal point is lost. Hence, explicit
casting needs to be done by the programmer.

QUE-4: What are the data-types and operators available in Java?


ANS:
1. Data Types: Java has two categories of data types:
o Primitive Data Types: These include byte, short, int, long, float, double, boolean,
and char.
o Non-primitive Data Types (Reference Types): These include classes, arrays, and
interfaces.
Assignment –1

2. Operators: Java provides a rich set of operators. Here are the main categories:
o Arithmetic Operators: These include +, -, *, /, %, ++, --.
o Relational Operators: These include ==, !=, >, <, >=, <=.
o Bitwise Operators: These include &, |, ^, ~, <<, >>, >>>.
o Logical Operators: These include &&, ||, !.
o Assignment Operators: These include =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=.
o Miscellaneous Operators: These include ? : (Ternary), instanceof, -> (Lambda).
Each operator and data type has its own rules and uses, which are defined in the Java Language
Specification. Understanding these is crucial to writing effective Java code.

QUE-5: What are syntax errors (compile errors), runtime errors, and logic
errors?
ANS:
1. Syntax Errors (Compile Errors): These are errors where the code isn’t written according
to the rules of the programming language. The compiler can’t understand the code, so it
can’t convert it into machine language. Examples include missing semicolons,
mismatched parentheses, undeclared variables, etc.
2. Runtime Errors: These errors occur during the execution of the program. The code is
syntactically correct, but it leads to illegal operations that make the program terminate.
Examples include dividing by zero, file not found, out-of-bounds array access, etc.
3. Logic Errors: These are errors where the program compiles and runs, but doesn’t
produce the expected output. This is due to the logic used in the code being incorrect.
These are often the hardest to debug because the compiler provides no indication of the
error.
Each type of error requires a different debugging approach. Syntax errors are usually
straightforward to fix once you understand the language’s syntax, while runtime errors require
understanding the program’s state at the time of error. Logic errors require a deep
understanding of the program’s intended behavior.

QUE-6: Why we declare main() method as public and static member.


ANS:
In Java, the main() method is declared as public and static for the following reasons:
Assignment –1

1. public: The main() method is public so that it can be accessible to the Java Virtual
Machine (JVM) which is outside the scope of our class where the main() resides. It’s the
entry point of any Java code and needs to be accessible from anywhere to start the
execution of the application.
2. static: The main() method is static because it needs to be called before an object of the
class is created. The static keyword allows the JVM to call the main() method without
creating an instance of the class.
Here’s how it looks in code:
public class Main {
public static void main(String[] args) {
// Your code here
}
}
In this code, public static void main(String[] args) is the entry point of any standalone Java
application. The main() method must be public and static so the JVM can call it to start the
program. The String[] args parameter represents command line arguments, an array of String
objects where each String represents an argument sent via the command line.

QUE-7: Discuss benefits of Object Oriented Approach?


ANS:
1. Modularity for easier troubleshooting: Something has gone wrong, and you have no
idea where to look. OOP makes it clear where to look.
2. Reuse of code through inheritance: Suppose that in addition to your Car object, one
colleague needs a RacingCar object, and another needs a Limousine object. Everyone
builds their objects separately but discover that they have used a lot of the same
properties. You can eliminate redundancy by making RacingCar and Limousine inherit
from the Car class.
3. Flexibility through polymorphism: This fancy word means that a single class can be used
to create many different objects. A single Car class can be used to instantiate as many
Car objects as you need.
4. Effective problem solving: OOP is good when you have fixed set of operations and the
object’s state is important. For example, GUI programming. You have buttons,
Assignment –1

checkboxes etc. These are all objects, with methods like draw(), onClick() etc. They all
have a state, like enabled, disabled etc. You can model them easily using OOP principles.

QUE-8: Explain features of java in detail.


ANS:
1. Simple: Java is easy to learn and its syntax is clear and concise. It is based on C++,
making it easier for programmers with a background in C++. Java has removed many
complicated and rarely-used features, such as explicit pointers and operator
overloading1.
2. Object-Oriented: Everything in Java is an object. This means we organize our software
as a combination of different types of objects that incorporate both data and behavior.
The basic concepts of Object-Oriented Programming (OOPs) in Java are Object, Class,
Inheritance, Polymorphism, Abstraction, and Encapsulation 1.
3. Platform Independent: Java is a write once, run anywhere language. Java code can be
executed on multiple platforms like Windows, Linux, Sun Solaris, Mac/OS, etc. Java code
is compiled by the compiler and converted into bytecode. This bytecode is a platform-
independent code1.
4. Secured: Java is known for its security. It allows the development of virus-free systems.
Java is secured because it has no explicit pointers and the programs run inside a virtual
machine sandbox1.
5. Robust: Java makes an effort to eliminate error-prone situations by emphasizing mainly
on compile-time error checking and runtime checking. It has a strong memory
management system1.
6. High Performance: Java is an interpreted language, so it will never be as fast as a
compiled language like C or C++. But, Java enables high performance with the use of
just-in-time compilers1.
7. Multithreaded: With Java’s multithreaded feature it is possible to write programs that
can perform many tasks simultaneously. This design feature allows the developers to
construct interactive applications that can run smoothly1.
These features make Java a powerful and popular programming language.

QUE-9: Compare compiler and interpreter.


ANS:

Compiler Interpreter
Assignment –1

Translates the entire program at once into Translates the program one statement at a
machine code12. time into machine code12.
The overall execution time is faster as the The overall execution time is slower as each
entire program is converted to machine code line is interpreted and executed one by
at once12. one12.
Generates object code which requires linking, No object code is generated, hence it is
hence requires more memory12. more memory efficient12.
Debugging is a bit difficult as it does not Debugging is easier as it executes line by
execute line by line12. line12.
Used in programming and development
Used in production environment1. environments1.
C, C++, Java2. JavaScript, Python, Ruby2.

QUE-10: Write a program to take string input as command line argument. In


addition, count occurrence of each character in a given string
ANS:

public class Main {


public static void main(String[] args) {
if(args.length > 0) {
String input = args[0];
int[] count = new int[256];
for (char c : input.toCharArray()) {
count[c]++;
}
for (int i = 0; i < 256; i++) {
if (count[i] > 0) {
System.out.println((char) i + ": " + count[i]);
}
}
} else {
System.out.println("Please provide a string as a command line
argument.");
}
}
}
Assignment –1

You might also like