PROGRAM CODING (JAVA)
Notes-
Question/Answers :-
Q.1) What is program ?
Ans1: A program is a set of instructions
written to solve a given problem.
Q.2) What are the major components
of a program ?
Ans2: Major components of a program
are : identifiers, literals, variables,
operators and commands.
Q.3) What are identifiers ?
Ans3: Identifiers are the names given
to identify different building blocks of a
program such as variable, class,
method etc.
Q.4) An identifier can start with the
underscore ________ . True/False ?
Ans4: True
Q.5 Which Of the following are valid
identifiers and why not :
•Data_rec - Valid
Reason - An identifier can have
underscore in it.
• _data - Valid
Reason - An identifier can start with
underscore.
• 1 data - Invalid
Reason - Identifier must not have
spaces and it cannot start with a
number.
• Data 1 - Invalid
Reason - Identifier must not have
space.
• [Link] - Invalid
Reason - Any other special character
except underscore and doller is not
allowed in identifiers.
• if - Invalid
Reason - Identifier must not be a
standard instruction of Java.
• switch - Invalid
Reason - Identifier must not be a
standard instruction of Java.
• while - Invalid
Reason - Identifier must not be a
standard instruction of Java.
• break - Invalid
Reason - Identifier must not be a
standard instruction oof Java.
Q.6 What are Literals?
Ans6 ‐ The actual data values that you
use in a program are called literals .
They can be of different types
depending upon the type of data, e.g. ‐
integer literal.
Q.7What is the function of operators?
Ans7. Operators are the symbols that
carry out some operation. Operators
are used to do calculations, frame a
condition or attach multiple conditions.
We need operators to form
expressions by applying them to the
variables and constants.
Q.8What are binary operators? Give
examples of arithmetic binary
operators.
Ans8. Operators that act upon two
operands are referred to as Binary
Operators.
Some Arithmetic Operators are :-
• Addition operator
Example – 4+20 results in 24.
• Subtraction operator
Example – 14-3 evaluates to 11.
• Multiplication operator
Example – a*c evaluates to 15.
• Division operator
Example – 100/5 evaluates to 20.
• Modulus Operator
Example – 19%6 evaluates to 1, since 6
goes into 19 three times with a
remainder.
Q.9 The expression 8%3 evaluates to.
Ans.9 - 2
Q.10 The modulus operator (%) can be
used only with the integer operands.
Ans.10 False
Q.11 What kind of programs are the
following:
Ans.11 • 13 - It is a integer literal
• 'a'‐ It is a character literal
• 4.38925 - It is a real literal
• "a" ‐ It is a string literal.
Q12: What do you understand by the
term Keyword?
Ans12: Keywords are reserved words
that have a special meaning for the
Java compiler. Java compiler reserves
these words for its own use so
Keywords cannot be used as identifiers.
For example, void, public, class, int ,
etc.
Q13. What is meant by precedence of
operators ?
Ans13. The hierarchical order in which
operators are used for different
operations is known as precedence of
operator.
E.g., Precedence of arithmetical
operators: braces, exponent,
division/multiplication,
addition/subtraction
Precedence of logical operators: NOT ,
AND, OR( ! , && , || )
Q14. Give one point of difference
between unary and binary operators.
Ans14. Unary operator is used to
perform operations on a single
operand, e.g. + , - , ++, -- etc. whereas
Binary operators perform operations
on two operands, e.g. +, - , * , / and %.
Q15. What is the difference between /
and % operator?
Ans15. The '/ ' operator results in a
quotient ( integer type ) when two
numbers are divided.
E.g. int a= 3 , b=7, c;
c = b/a;
The output: c = 2
The ' % ' operator results in a
remainder ( integer type ) when one
number is divided by other.
E.g. int a = 3, b=7, c;
c = b/a
The output: c = 1
SOME EXTRA QUESTIONS-
[Link] is the old name of java ?
Ans1. The old name of Java is Oak .
2. Who is the father of java ?
Ans. James Gosling is the father of
Java.
3. What is the file extension of java?
Ans. The file extension of java is .java .
4. When was java discovered ?
Ans. Java was discovered in 1996.
5. Name two developers of java.
Ans. Mike Sheridan and Patrick
Naughton are the other two
developers of Java.
STRUCTURE OF A JAVA PROGRAM
1. COMMENT SECTION-
It comprises of a comment lines which
gives the name of the program. Single
line comment ( // ) and multiline
comment ( /*........ */ ).
2. IMPORT STATEMENTS-
The next is the number of import
statements , which is equivalent to
#include statement in C++.
Example: [Link]*;
3. CLASS DEFINITION-
A Java program may contain multiple
class definitions. These are the
important elements of any Java
program. They are used to plot the
objects of real word problem .
4. public static void main ()
• The word public indicates it is
accessible by any other classes.
• The word static means that it is
unique .
• The word void means this main ( )
method has no return value.
• main ( ) is a method where the
program starts.
5. STRING ARGS [ ]
This is the last statement called as
command line argument; args is an
array of string objects representing
arguments passed to the class at the
time of execution.
6. [Link]( )
This is the output statement that prints
or displays any message or result on
the output screen . To combine two or
more data items in a single line , we
use the symbol ' + ' symbol.
IMPORTANT TABLE TO BE NOTED -