Answer Of Questions
1. Write Java program to calculate factorial of a user entered number.
Here is how you would construct the program based on the loops and keyboard input concepts taught in the book:
import java.io.DataInputStream;
class Factorial {
public static void main(String args[]) {
DataInputStream in = new DataInputStream(System.in);
int n = 0;
long fact = 1;
try {
System.out.println("Enter an Integer: ");
n = Integer.parseInt(in.readLine());
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
System.out.println("Factorial of " + n + " is " + fact);
} catch (Exception e) {
System.out.println("Error reading input.");
}
}
}
2 & 5. Explain Switch Statement in Java.
The switch statement is a "Selection Statement" in Java. It is used to select one of several control flows. The switch statement allows a variable to be tested for equality against a list of values, offering a more streamlined way to handle multi-way branches than using nested if...else statements.
3 & 11. Explain features of Java.
The inventors of Java wanted to design a language that could offer solutions to modern programming problems while being reliable, portable, distributed, simple, compact, and interactive. Sun Microsystems officially describes Java with the following attributes:
- Compiled and Interpreted: Java combines both approaches. The compiler translates source code into intermediate bytecode instructions, and the Java interpreter generates machine code that can be directly executed.
- Platform-Independent and Portable: Java programs can be easily moved from one computer system to another. Changes in operating systems or processors do not force changes in Java programs.
- Object-Oriented: Almost everything in Java is an object. All program code and data reside within objects and classes.
- Robust and Secure: It provides safeguards to ensure reliable code, such as strict compile-time and run-time checking, and garbage collection. Its lack of pointers ensures programs cannot gain unauthorized access to memory.
- Distributed: It has the ability to share both data and programs over networks.
- Familiar, Simple, and Small: Redundant or unreliable features of C and C++ (like pointers and goto statements) were not included in Java.
- Multithreaded and Interactive: It can handle multiple tasks simultaneously.
- High Performance: Java speed is comparable to native C/C++.
- Dynamic and Extensible: It can dynamically link in new class libraries, methods, and objects.
4 & 12. Discuss Relational Operators.
Relational operators are used to compare two quantities, and depending on their relation, take certain decisions. The value of a relational expression is either true or false. Java supports six relational operators:
<(is less than)<=(is less than or equal to)>(is greater than)>=(is greater than or equal to)==(is equal to)!=(is not equal to)
6. Explain Inheritance with example.
Inheritance is the process by which objects of one class acquire the properties of objects of another class. Inheritance supports the concept of hierarchical classification and provides the idea of reusability. This means that we can add additional features to an existing class without modifying it by deriving a new class (known as a subclass) from the existing one.
Example: The book uses the example of birds. The bird "robin" is a part of the class "flying bird", which is again a part of the class "bird". Each derived class shares common characteristics with the class from which it is derived.
7. Define the following terms.
a) class: A class may be thought of as a 'data type' and is a collection of objects of a similar type. The entire set of data and code of an object can be made a user-defined data type using the concept of a class. Classes use the concept of abstraction and are defined as a list of abstract attributes and methods that operate on these attributes.
b) object: Objects are the basic runtime entities in an object-oriented system. They take up space in the memory and have an associated address. Each object contains data and code to manipulate the data. An object is considered to be a partitioned area of computer memory that stores data and a set of operations that can access the data.
8 & 13. Why Java is called platform independent language.
Java is platform-independent because Java programs can be easily moved from one computer system to another, anywhere and anytime. Changes and upgrades in operating systems, processors, and system resources will not force any changes in Java programs. This portability is ensured in two ways: first, the Java compiler generates intermediate bytecode instructions that are machine-independent and can be implemented on any machine; secondly, the sizes of primitive data types are machine-independent.
9 & 14. Explain Java Virtual Machine.
All language compilers translate source code into machine code for a specific computer, but Java compiler produces an intermediate code known as "bytecode" for a machine that does not exist. This machine is called the Java Virtual Machine (JVM) and it exists only inside the computer memory. It is a simulated computer within the computer and does all major functions of a real computer. The JVM acts as the intermediary between the operating system and the Java object framework.
10 & 15. Explain Arithmetic operator in detail.
Arithmetic operators are used to construct mathematical expressions as in algebra. Java provides basic arithmetic operators that can operate on any built-in numeric data type (except boolean). The operators are:
+(Addition or unary plus)-(Subtraction or unary minus)*(Multiplication)/(Division)%(Modulo division / Remainder)
There are two main types of arithmetic:
- Integer Arithmetic: When both operands are integers, it yields an integer value. For example, integer division truncates the decimal part (e.g., 14 / 4 = 3).
- Real Arithmetic: Operations involving only real operands. Unlike C and C++, Java's modulus operator
%can also be applied to floating-point data to return a floating-point remainder.
(Note: Mixed-mode arithmetic occurs when one operand is real and the other is integer; the integer is promoted to a real before the operation is performed.)