Oop Assignment 1
Oop 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
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.
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.
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
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
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.
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.
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.
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.