CHAPTER 2
Introduction to Java
Section 3: Assignment Questions
1. Write a short note on types of Java programs.
Ans. There are two types of java programs:
Stand-alone : Stand alone java application refers to java program that can run independently on a
computer.
Applets : Java applications that run inside a web browser.
2. Which language does the CPU understand?
Ans. CPU understands machine language.
3. Describe the traditional compilation process with a suitable diagram.
Ans.
4. Describe the Java compilation process with a suitable diagram.
Ans.
5. What is the difference between an interpreted language and a compiled language?
Ans. The interpreted language is the source code instructions translated into machine code line-by-line,
whereas compiled language is the source code written in a high-level language which gets converted
into machine code all at once before the program is executed.
6. How is traditional compilation process different from Java compilation process?
Ans. In the traditional compilation process, an equivalent machine code is generated for that particular
platform however, on compiling the Java source code, you get a Bytecode which cannot be directly
executed by the machine. This Bytecode needs a Java Interpreter to convert it into the machine code of
the computer on which it is to be executed. This interpreter is part of the Java Virtual Machine (JVM).
7. What are JVM and JIT?
Ans. A Java Virtual Machine (JVM) is a virtual machine that enables a computer to run Java Program. The
JVM includes a Just-In-Time (JIT) compiler that compiles selected portions of Bytecode into executable
code.
8. Distinguish between the following:
a. Source code and Object code
Ans. A set of instructions written in a high-level programming language is called source code.
A compiler generated machine language version of the source code is called object code.
b. Compiler and Interpreter
Ans. A compiler is a program that translates code written in a high-level language into machine code.
An Interpreter translates the source code instructions into machine code one-by-one.
c. ".java" file and ".class" file
Ans. The source code is written using the Java language. The files are saved with the “.java” extension.
The “.java” files are compiled using the Java compiler.
The compilation process generates Bytecode files with the “.class” extension.
9. Describe the slogan, "Write once, run anywhere" in relation to Java.
Ans. The concept of “Write once, run anywhere”, is one of the most important features of Java, which
makes it a very powerful language. Java applications can run on any platform that has the JVM installed
on it.
10. Describe four features of Java.
Ans. Following are some of the features of Java:-
i. Platform independent – The concept of “Write once, run anywhere”, is one of the most important
features of java, which makes it a very powerful language. Java applications can run on any platform
[Link]
that has the corresponding JVM installed on it.
ii. Simple – Java is a simple and easy to learn programming language. The removal of many unreliable
features of C and C++ (its predecessors) has simplified its understanding and implementation.
iii. Robust – Java is a robust and reliable programming language. Its powerful compiler checks for early
errors during compilation and the interpreter checks for any runtime errors thus, eliminating situations
that are error-prone.
Introduction to Java ~2~
iv. Secure – Java is secure because it allows Applets to confine to the Java runtime environment only.
Applets cannot access any other part of the computer when downloaded on the web browser. This
helps to eliminate any damage due to malicious software and viruses. For example, accessing
passwords, or finding your bank details by searching the contents of your computer.
11. What is the difference between machine code and Bytecode?
Ans. Computers were developed to carry out instructions that were written in its native language called
machine language. Machine language instructions are expressed as binary numbers called machine
code.
The programs written in Java are compiled into a machine language for a virtual computer called Java
Virtual Machine (JVM). The machine language for this special Java Virtual Machine is called Java
Bytecode. The Bytecode files are generated with the ".class" extension.
12. What role does the Java Virtual Machine play in the compilation and interpretation process?
Ans. Java compilation process is different. Here, the machine code is for a virtual machine known as Java
Virtual Machine or JVM. The machine code for the Java Virtual Machine is called Bytecode. This
Bytecode needs a Java Interpreter to convert it into the machine code of the computer on which it is to
be executed. This interpreter is a part of the Java Virtual Machine. The Java source code file has a ".java"
extension, and the Bytecode file has a ".class" extension.
A different Java Bytecode Interpreter is needed for each type of computer (i.e., Windows, UNIX, and
Macintosh). However, once the computer has the required Java Bytecode Interpreter, it can run any
Java Bytecode program. The JVM also includes a Just-In-Time (JIT) compiler that compiles selected
portions of Bytecode into executable code. The remaining code is simply interpreted.
13. Can Java run on any machine? What is required to run Java on a computer?
Ans. Java applications can run on any platform that has the corresponding JVM installed on it.
[Link]
Introduction to Java ~3~