Professional Documents
Culture Documents
Tapas Sir Notes
Tapas Sir Notes
Ch 2: INTRODUCTION TO JAVA
1. What was Java originally known as? Who developed Java?
Ans. Java was originally known as OAK. It was developed by James Gosling .
2. What are the characteristics of Java?
Ans: The characteristics of Java as follows:
i) It is an object oriented language, simple and robust.
ii) Java program is both compiled and interpreted.
iii) Write once run anywhere (WORA). The java programs are written once, but can be run on different
platform without making changes.
iv)Has enhanced security features.
v)Platform independent.
3. Name two types of Java programs.
Ans. Application and Applet
4. Explain the two types of Java programs.
Ans: Two types of Java programs are Applets and Stand alone applications. i. Applets – small programs
that are embedded in web pages and are run on the viewer‟s machine by Java capable sc owsers.
Used for games and simulation programs. ii. Standalone application- includes the software
applications that do not require low level operating system or hardware access. This includes most
desktop applications like word processors and spreadsheets
5. What is Java API?
Ans. An application programming interface (API), in the context of Java, is a collection of prewritten
packages, classes, and interfaces with their respective methods, fields and constructors.
6. Define the term byte code.
Ans. Java compiler converts Java source code into an intermediate binary code called Bytecode.
Bytecode can't be executed directly on the processor. It needs to be converted into Machine Code
first.
7. What is the difference between source code and object code?
Ans.
Source Code Object Code
It is a set of statements in a High-Level It is a set of statements in Machine
programming language. Language.
It is understood by human/programmer. It is understood by the processor.
8. How does ordinary compilation take place?
Ans. The process of converting the source code into machine code is called compilation. The
compilation produces the machine code. For different platforms different machine codes are produce.
e.g Windows compiler produces an object code that run in Windows computer only.
9. Briefly explain the compilation process of Java source code.
Ans. Compilation in java: i. Java programs are written in “.java” files and are compiled by the compiler
to produce the bytecode (.class file) ii. The JVM is a virtual machine which reads the byte code and
interprets into machine code(object code)
10. What is Java Virtual Machine (JVM)?
Ans. Java Virtual Machine (JVM) is a software that takes Bytecode as input, converts it into Machine
code of the specific platform it is running on and executes it. JVM is platform specific, each platform
has its own JVM.
11. State the difference between Applet and Application.
Ans. The fundamental difference between the two Java programs is that an application program is
designed to run on a stand-alone machine whereas an applet is a web-version of an application which
is used to run a program on a web browser
12. What is package?
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java.lang, Java.io, java.util etc.
A Class An Object
Class is a blueprint or template from Object is an instance of a class.
which objects are created.
Class is a group of similar objects. Object is a real world entity such as pen,
laptop, mobile, bed, keyboard, mouse,
chair etc.
Class is a logical entity. Object is a physical entity.
5. How do objects communicate with each other?
Ans. Objects communicate with each other through message passing.
6. How can you implement abstraction in programming terms?
Ans: Data abstraction is a method where essential elements are displayed to the user and trivial
elements are kept hidden. In Java, abstraction is achieved by using the abstract keyword for classes
and interfaces. In abstract classes, we can have abstract methods as well as concrete methods.
7. how can you implement inheritance in programming terms?
Ans: Inheritance is the procedure in which one class inherits the attributes and methods of another
class. The class whose properties and methods are inherited is known as the Parent class or base
class. And the class that inherits the properties from the parent class is the Child class or sub classs.
You use the keyword extends to identify the class that your subclass extends.
8. How is encapsulation implemented in a program?
Ans: Encapsulation in Java is the process by which data (variables) and the code that acts upon them
(methods) are integrated as a single unit. By encapsulating a class's variables, other classes cannot
access them, and only the methods of the class can access them.
9. How can you implement polymorphism in java?
Ans: Polymorphism in Java can be achieved in two ways i.e., method overloading and method
overriding. Polymorphism in Java is mainly divided into two types. Compile-time polymorphism can
be achieved by method overloading, and Runtime polymorphism can be achieved by method
overriding.
10. Why is a class called a user-defined data type?
Ans: Primitive data types are the general and fundamental data types that we have in Java and those
are byte, short, int, long, float, double, char, boolean, etc., User defined data types are those that
user / programmer himself defines.
11. State two differences between fundamental and user-defined data type.
Fundamental data type User defined data type
These are inbuilt data type provided by These are data types created by the user
the Java Language. using fundamental or user defined data
type or both.
The size of it is fixed. The size of different user-defined data
type depends upon the size of the
individual components of it
These data types are available in all parts These data types are available only as
of a program within a class. specified by the access specifiers.
12. What is an access specifier? Which two access specifier is used in a class
declaration?
Ans. Access modifiers (or access specifiers) are keywords in object-oriented languages that set the
accessibility of classes, methods, and other members. Access modifiers are a specific part of
programming language syntax used to facilitate the encapsulation of components. The public and
default access specifier is used in a class declaration.
13. Why is a class called an object factory?
Ans. A class is called an object factory because objects are created from a class. An object is an
instance of a class.
The following statements create two objects s1 and s2 of the class Student.
Student s1 = new Student();
Student s2 = new Student();
So, we have a single class Student but we can create as many objects as we want (like s1, s2, etc.)
from that single class.
This is similar to what happens in a factory. Consider a factory which produces car. They have only a
single design of a car but produce multiple cars from that single design.
Things are similar in the world of classes and objects. There is a single definition of a particular class
(like Student) but we can produce many Student objects (like s1, s2) from that single class.
14. Why is an Object called an 'Instance' of a class? Explain.
Ans. A class can create objects of itself with different characteristics and common behaviour. So, we
can say that an Object represents a specific state of the class. For these reasons, an Object is called an
Instance of a Class.
15. Why is a class known as composite data type?
Ans. A class can contain data members of various primitive and reference data types. Hence, class is
known as composite data type.
16. How will you define a software object?
Ans. A software object replaces the characteristics and behaviours of a real-world object with data
members and member methods, respectively.
17. State two rules you should follow for naming a class or Identifier.
Ans. While using names for a class the following set of rules are to be kept in mind.
1. It can have any alphabet (capital or small), digits, underscore and dollar sign characters. For
example, a, b, cat, mat123, cost_price, Topaz$ are all example of valid identifier.
2. It should not begin with digits or should not contain any special character. For example 2ab, ab#c,
top@, etc., are invalid identifiers as it either begins with a digit or contain special characters (like #,
@).
3. It cannot have a space between it. For example, Simple Interest or Selling Price are invalid class-
names as it contains a space.
4. It must not be a keyword. For example, for, while, do are invalid class-names as they are keywords
and are assigned some special function for the language.
5. It can be of any length. Even though Java gives you the flexibility to provide a huge length for an
identifier a very long name is impractical and difficult to manage.
Identifiers
Literals
Operators
Separators
Keywords
2. What is an identifier? What is the identifier formatting rule of Java? OR What are the
rules for naming a variable?
Ans: Identifiers are names given by the programmer to different parts of a program e.g. variables,
functions, classes,packages etc.
The rules of naming identifiers in Java.
(i) Can contain alphabets, digits, dollar sign and underscore.
(ii) Must not start with a digit.
(iii) Cannot be a Java keywords.
(iv) Can have any length and are case-sensitive.
3. What is a literal? What are the different types of literals available in Java?
Ans. Literal is a constant value that can be assigned to a variable. The different types of literals in Java
are:
1. Integer-literal or Fixed point-literal
2. Floating point-literal
3. Boolean-literal
4. Character-literal
5. String-literal
6. Null-literal
7. Class literal
4. What are keywords? Can keywords be used as a identifiers?
Ans: Keywords are the reserved words that convey a special meaning to the language compiler. No,
keywords are reserved for special purpose and must not be used as identifiers
5. State the difference between a boolean literal and a character literal.
Ans. A boolean literal may be either true or false and character literal are always enclosed within
single quotes.
6. Write any two escape sequences used in Java.
Ans. For newline :\n
For tab: \t
7. What are the different punctuators available in Java?
Ans. There are 9 punctuators available in Java.
They are: ( ) { } [ ] ; , .
8. Differentiate the two kinds of data types?
Ans: The two types of data types are: Primitive and non-primitive/composite/user define data types.
The primitive data types are the basic datatypes provided by the language. It is used to define and
hold a value in a named variable. They are: byte, short, int, long, float, double, char and Boolean.
The non-primitive/reference data types are created using primitive datatypes. It is used to store the
memory address of an object. The reference datatypes are: class, array and interface.
9. How many bytes occupied by the following data types: byte, short, int, long, float,
double, char, boolean.
Ans: char-2 byte, byte-1 byte, short-2 bytes, int-4 bytes, long-8 bytes, float-4 bytes, double-8 bytes,
booleanJava reserve 8 bits but only use 1 bit.
10. Define variable with an example.
Ans. A variable represents a memory location through a symbolic name which holds a known or
unknown value of a particular data type. This name of the variable is used in the program to refer to
the stored value.
Example:
int mathScore = 95;
11. What do you mean by constant? Explain with an example.
Ans. The keyword final before a variable declaration makes it a constant. Its value can't be changed in
the program.
Example:
final int DAYS_IN_A_WEEK = 7;
12. Explain the term 'type casting'?
Ans. The process of converting one predefined type into another is called type casting.
13. What is the range of the following data types: byte, short, int, long, float, double,
char, boolean.
Ans: byte -2 7 to +27 -1 short -2 15 to 215 -1
int -2 31 to 231 -1 long -2 63 to 263 -1
float -3.4×1038 to 3.4×1038 double -1.7×10308 to 1.7×10308
char 0 to 65536 boolean true or false
14. Name two keywords which are also literals in Java.
Ans. They are null, true and false.
15. What do you understand by variable scope?
Ans. Variable scope is the area in which a variable is accessible or have its life.
16. Define the following:
a) Implicit type conversion
In implicit type conversion, the result of a mixed mode expression is obtained in the higher most data
type of the variables without any intervention by the user. Example:
int a = 10;float b = 25.5f, c;c = a + b;
(b) Explicit type conversion
In explicit type conversion, the data gets converted to a type as specified by the programmer. For
example:
int a = 10;double b = 25.5;float c = (float)(a + b);
17. What is Coercion?
Ans. we can say that coercion is the process of converting one data type to another data type. In a
more specific way, implicit conversion is called coercion. It is also known as type coercion.
18. Difference between primitive and non-primitive data types
The difference between primitive and non-primitive data types are as follows:
Primitive types are predefined in Java. Non-primitive types are created by the programmer and is
not defined by Java.
Non Primitive types can be used to call methods to perform certain operations, while primitive
types cannot.
A primitive type always has a value, whereas non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase
letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same
size.
== Equal to
!= Not Equal to
/*
* All statements within this set of braces
* form the compound statement
*/
Math.ceil( ) Math.floor( )
Returns the smallest double value that is greater than Returns the largest double value that is less than or
or equal to the argument and is equal to a equal to the argument and is equal to a mathematical
mathematical integer integer.
double a = Math.ceil(65.5); double b = Math.floor(65.5);
In this example, a will be assigned the value of 66.0 as In this example, b will be assigned the value of 65.0 as
it is the smallest integer greater than 65.5. it is the largest integer smaller than 65.5.
= ==
It is the assignment operator used for assigning a value It is the equality operator used to check if a variable is
to a variable. equal to another variable or literal.
E.g. int a = 10; assigns 10 to variable a. E.g. if (a == 10) checks if variable a is equal to 10 or not.
8. What are logical operators? Explain each of them with the help of an example.
Ans. Logical Operators are used to join more than one relational expression. The following statements explains the
usage of logical operators:
• If I have a pen AND a paper it is possible for me to write. This is an example of AND join, where both the conditions
need to be satisfied for the resultant to be true. Thus, only when I have both, pen and paper it will be possible for
me to write. Thus, AND operator is used when all conditions need to be satisfied.
• If I have a pen OR a pencil it is possible for me write. This is an example of OR join, where either of the conditions
need to be satisfied for the resultant to be true. Thus, if I have a pen I can write or if I have a pencil still I can write.
Thus, OR operator is used when either of the conditions need to be satisfied.
• If it is NOT that, I do not have a pen, it is possible for me to write. Here see how the NOT operator is used to
negate a condition.
9. What is the function of nested if?
Ans. A nested if is an if statement that is the target of another if statement. Nested if statements means an if
statement inside another if statement. This is generally used when a condition needs to be satisfied inside another
condition.
10. What is a conditional/Ternary operator? Explain with the help of an example.
Ans. This operator is used as an alternative to if statement. The ? and : are used as conditional or ternary operator.
This operator can be used to replace if else statements of the general form:
if (condition)
statement1;
else
statement2;
The above form of if can be alternatively written using ? and : as follows:
condition? statement1 : statement2;
It works in the same way as the above given form of if. That is test-condition is evaluated and if it is true, statement1
gets executed (i.e. its value becomes the value of entire expression) otherwise statement2 gets executed (i.e. its
value now becomes the value of the entire expression). For
instance, the following if statement
int c;
if (a>b)
c=a;
else
c=b;
can be alternatively written as
int c;
c = ((a>b) ? a : b);
11. Compare if and ? : .
If-else Ternary Operator
It is used when complex logic handling is necessary. It is used to offer more concise, clean and compact
code
It can have multiple statements, multiple assignments It produces an expression, and hence a single value
and expressions (more than one statements) in its can, be assigned or incorporated into a larger
body. expression.
Nested if statement is simple and fairly easy to Ternary operator in its nested form, it becomes
understand. complex and difficult to understand.
12. What is the problem of dangling-else? When does it arise? What is the default danglingelse matching and how it
is overridden?
Ans: The nested if-else statement introduces a source of potential ambiguity referred to as dangling-else problem.
This problem arises when in a nested if statement, number of if’s is more than the number of else clauses. The
question then arises, with which if does the additional else clause property match.
For Example:
if(ch>=”A”)
if(ch<=‟Z‟)
++upcase;
else
++other;
The indentation in the above code fragment indicates that programmer wants the else to be with the outer if.
However, Java matches an else with the preceding unmatched if. One method for overriding the default dangling-
else matching is to place the last occurring unmatched if in a compound statement, as it is shown below.
if(ch>=”A”)
{
if(ch<=‟Z‟)
++upcase;
}
else
{
++other;
}
13. What is a switch statement? How is a switch statement executed?
Ans: switch statement in a program, is used for multi-way branch. It compares its expression to multiple case values
for equality and executes the case whose value is equal to the expression of switch. If none of the cases match,
default case is executed. If default case is absent then none of the statements from switch are executed.
The syntax :
switch(expression)
{
case constants : statements;
break;
case constants : statements;
break;
default: statements;
break;
}
14. Explain with the help of an example, the purpose of default in a switch statement.
Ans: When none of the case values are equal to the expression of switch statement then default case is executed. In
the example below, value of number is 4 so case 0, case 1 and case 2 are not equal to number. Hence sopln of
default case will get executed printing "Value of number is greater than two" to the console.
int number = 4;
switch(number) {
case 0:
System.out.println("Value of number is zero");
break;
case 1:
System.out.println("Value of number is one");
break;
case 2:
System.out.println("Value of number is two");
break;
default:
System.out.println("Value of number is greater than two");
break;
}
15. What is a control variable in a switch case?
Ans: A control variable in switch case is one which guides the control to jump on a specified case.
e.g. switch(x), here “x” is the control variable.
switch if-else
switch can only test if the if-else can test for any boolean expression
expression is equal to any of its like less than, greater than, equal to, not
case constants equal to, etc.
i. switch can only test for equality whereas if can test for any Boolean expression.
ii. switch tests the same expression against constant values while if-else-if ladder can use different expression
involving unrelated variables.
iii. switch expression must only evaluate to byte, short, int, char, String or an enum. if doesn’t have such limitations.
iv. A switch statement will run much faster than the equivalent program written using the if-else-if ladder