0% found this document useful (0 votes)
41 views36 pages

Chapter 1 UNIT I TO V (B)

Uploaded by

Divyashree R H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views36 pages

Chapter 1 UNIT I TO V (B)

Uploaded by

Divyashree R H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter 1 - Unit 1 TO Unit V(b)

Chapter 1 - Unit 1
Introduction to Object Oriented Programming Concepts
Multiple Choice Questions
Question 1
In object oriented programming, the stress is given on:
1. procedure
2. methods
3. class
4. data
Answer
data
Reason — In Object Oriented Programming, the stress is put on the data values rather than
functions.
Question 2
Which of the following principle does not allow to access directly from outside the class premises?
1. data hiding
2. encapsulation
3. abstraction
4. all of the above
Answer
all of the above
Reason — Data hiding, Encapsulation and Abstraction, all the three restrict direct access to data
from outside a class.
Question 3
The process of combining data and functions that enables them to be together as a single entity is
called
1. inheritance
2. encapsulation
3. classification
4. attributes
Answer
Encapsulation
Reason — Wrapping of data and functions of an object as a unit that can be used together in a
specific operation is known as Encapsulation.
Question 4
The process by which a class acquires the properties from another class is called:
1. polymorphism
2. inheritance
3. abstraction
4. object
Answer
Inheritance
Reason — Inheritance is a property by virtue of which one class acquires some features from another
class.
Question 5
In procedural programming, the stress is laid on:
1. class
2. function

Page 1 of 36
3. data
4. object
Answer
Function
Reason — A programming language in which emphasis is given on the functions rather than data
values is known as Procedure Oriented Programming Language.
Question 6
Which of the following is a blue print that creates objects of similar types?
1. abstraction
2. encapsulation
3. class
4. function
Answer
class
Reason — Class is a blue print that creates objects of similar types.
Question 7
Which of the following is not an object oriented programming language?
1. C++
2. Simula
3. BASIC
4. Java
Answer
BASIC
Reason — BASIC is a procedure oriented programming language.
Question 8
The process of restricting the free flow of data from the outside world is known as:
1. encapsulation
2. inheritance
3. function
4. class
Answer
encapsulation
Reason — The process of restricting the free flow of data from the outside world is known as
encapsulation.
Fill in the blanks
Question 1
Object is the fundamental concept in object oriented programming language.
Question 2
Objects can communicate with each other through function in object oriented programming
language.
Question 3
Using a function for multiple operations is called as polymorphism.
Question 4
Data Abstraction is an act of representing essential features without including background details.
Question 5
Feature of wrapping data and functions as a single unit is called encapsulation.
Question 6
Inheritance promotes the reusability feature, in object oriented programming.
Write short answers

Question 1

Page 2 of 36
What is object oriented programming? Name two object oriented programming languages.
Answer
Object Oriented Programming is an approach in which stress is laid on data rather than functions.
The data values remain associated with the functions of a particular block of the program so as to
encourage data security.
Two object oriented programming languages are C++ and Java.

Question 2
Name four basic principles of object oriented programming.
Answer
Four basic principles of object oriented programming are:
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
Question 3
Why do we prefer object oriented approach in complex programming? Explain.
Answer
Object Oriented approach offers advantages like:
1. Data Values are secured.
2. Mishandling of data is protected.
3. Error detection and correction becomes easier.
4. Easier in coding complex programs.
For these reasons, Object Oriented approach is preferred for complex programming.
Question 4
What is meant by a base class and a derived class?
Answer
The class that is inherited is known as base class and the class that inherits from the base is known as
derived class.
Question 5
Mention two limitations of procedure oriented programming approach.
Answer
Two limitations of procedure oriented programming approach are:
1. No restriction on data values.
2. No reusability concept hence time management, testing and length of the program increases.
Question 6
What is meant by Encapsulation?
Answer
Wrapping of data and functions of an object as a unit that can be used together in a specific operation
is called Encapsulation.
Question 7
Define the following with an example each.
(a) Inheritance
(b) Polymorphism
Answer
(a) Inheritance is a property by virtue of which one class acquires some features from another class.
It promotes reusability.
As an example of inheritance, we can consider the case of vehicles. All vehicles have some common
features like they can move on the road and transport people and goods from one place to another.
These vehicles differ from each other in certain aspects like whether it transports passengers or
goods, how many passengers it can accommodate at a time, whether it is a two-wheeler or four-
wheeler, etc. So, we have different types of vehicles like Cars, Bikes, Scooters, Auto rickshaw,

Page 3 of 36
Buses, Trucks, etc. Using inheritance, we can make vehicles the base class with the different types of
vehicles being the derived class as shown below:

(b) Polymorphism is the process of using a function for carrying multiple operations. During this
process, an object may include a function for multiple operations.
As an example, if we ask different animals to speak, they respond in their own way. Dog will bark,
duck will quack, cat will say meow and so on. So, the same action of speaking is performed in
different ways by different animals. This is the concept of Polymorphism.
Question 8
In what way is Data Hiding related to Data Abstraction?
Answer
Data Hiding and Data Abstraction are complementary concepts. Data Abstraction focuses on the
observable behaviour of an object, whereas Data hiding or Data Encapsulation focuses upon the
implementation that gives rise to this behaviour. In other words, Data Abstraction cares about what
something does but not how it does it. Data Encapsulation cares about how something does what it
does such that others don't have to worry about the implementation details. Hence, we can say that
Encapsulation is a way to implement Data Abstraction.
Question 9
Give an example to explain Data Abstraction.
Answer
Data abstraction is an act of representing the essential features without knowing the background
details.
As an example, lets consider the electrical switchboard. Switchboard provides us a very simple way
to switch ON/OFF lights, fans and other electrical appliances. It hides all the details like the internal
wiring of the house, how the switch is turning the light ON/OFF, etc.
Question 10
What is meant by Data Hiding?
Answer
The insulation of data that does not allow it to be accessed directly outside the class premises,
although it is available in the same program is known as Data Hiding.
Question 11
Which of the Object Oriented programming principles explain the following illustrations? Justify.
(a) The variables and methods are put together in a (b) A man withdrawing money from
Class. ATM.

Answer
(a) Encapsulation is illustrated in the given picture. Wrapping of data and functions of an object as a
unit (class) that can be used together in a specific operation is known as Encapsulation. In the
picture, the variables and methods are being encapsulated in a class.

Page 4 of 36
(b) The principle of Data abstraction is illustrated here as the man withdraws money from the ATM
using the withdraw option. He knows the essential features of the ATM machine such as
withdrawing money, without knowing the complex background details of how the ATM machine is
operating to provide money to him.

Chapter 1 - Unit 2
Elementary Concept of Objects and Classes
Fill in the blanks
Question 1
Creating object is the fundamental concept in object oriented programming language.
Question 2
A class is also considered as an object factory.
Question 3
A real world object deals with characteristics and behaviours.
Question 4
The object of a class differs on various characteristics.
Question 5
The characteristics of the real world objects are considered to be the data members of
the software objects.
Question 6
An object of a class is created by using a keyword new.
Question 7
Class is a blueprint of the objects.
Question 8
new keyword is used for dynamic allocation of an object.
Question 9
The living things are considered as real world objects.
Question 10
The different objects of a class have common behaviours.

EXTRA MAIN
Multiple Choice Questions
Question 1
Which of the following keywords is used to create an instance of a class?
1. new
2. public
3. class
4. main
Answer
new
Reason — New keyword is used to create an instance of a class.
Question 2
The ............... is called an instance of a class.
1. attribute
2. object
3. state
4. features
Answer
object
Reason — The data members declared within a class are known as instance variables. When an
object of a class is created, it includes instance variables described within the class. Hence, an object
is called as an instance of a class.
Page 5 of 36
Question 3
The objects we see around us are called as:
1. Real world object
2. Virtual object
3. Imaginary object
4. None of the above
Answer
Real world object
Reason — The objects we see around us are called as Real world objects.
Question 4
Which of the following makes a function non-returnable?
1. public
2. static
3. void
4. new
Answer
void
Reason — The keyword 'void' makes a function non-returnable.
Question 5
Which of the following statements is valid for the objects?
1. They possess same characteristics and behaviour.
2. They possess same characteristics but different behaviour.
3. They possess different characteristics and different behaviour.
4. They possess different characteristics but common behaviour.
Answer
They possess different characteristics but common behaviour.
Reason — Objects are the entities which possess different characteristics and common behaviour
described within the class.
Answer the following questions
Question 1
How will you define a software object?
Answer
A software object replaces the characteristics and behaviours of a real world object with data
members and member methods, respectively.
Question 2
Define class and object with an example.
Answer
An object is an entity having a specific identity, specific characteristics and specific behavior.
A class is a blue print that represents a set of objects that share common characteristics and
behaviour.
Take the example of a house. An architect will have the blueprints for a house. Those blueprints will
be plans that explain exactly what properties the house will have and how they are all laid out.
However, it is just the blueprint, we can't live in it. Builders will look at the blueprints and use those
blueprints to make a physical house. They can use the same blueprint to make as many houses as
they want. Each house will have the same layout and properties. Each house can accommodate its
own families. So, in this example, the blueprint is the class, the house is the object and the people
living in the house are data stored in the object's properties.
Question 3
What does the following statement mean?
Employee staff = new Employee ( );
Answer
This statement creates a new object of class Employee. The newly created object is assigned to a
variable named staff which is of Employee type. The object can be accessed using staff variable.
Page 6 of 36
Question 4
A class is also referred to as 'Object Factory'. Comment.
Answer
A class has the complete description of the data elements the object will contain, the methods the
object can do, the way these data elements and methods can be accessed. A class is used to create
similar objects that possess different characteristics and common behaviour. Hence, class is called an
object factory.
Question 5
Why is a class known as composite data type?
Answer
When a user creates a class, it becomes a data type for his program. This data type includes different
primitive types such as int, float, char, etc. Thus, class is referred to as a user defined data type or
Composite Data Type.
Question 6
A statement is given as:
'Study_Table' is an object of the class 'Furniture'. Write down Java statement for the same.
Answer
Furniture Study_Table = new Furniture();
Question 7
Class and Objects are inter-related. Explain.
Answer
A Class is used to create various Objects that have different characteristics and common behaviours.
Each object follows all the features defined within a class. That is why class is also referred to as a
blue print or prototype of an object. This way we can say that they are inter-related.
Question 8
Why is an Object called an 'Instance' of a class? Explain.
Answer
The data members declared within a class are also known as instance variables. When an object of a
class is created, it includes instance variable described within the class. This is the reason that an
object is called an instance of a class.
Question 9
Write a statement to create an object 'Keyboard' of the class 'Computer'.
Answer
Computer Keyboard = new Computer();
Question 10
Mention three characteristics and two methods for the following Classes:
(a) class Mobile Phone
(b) class Bike
(c) class Fruits
(d) class Pen
Answer
(a) class Mobile Phone (b) class Bike
Characteristics Methods
colour makeCall()
model receiveCall()
IMEI Number

Page 7 of 36
Characteristics Methods
colour startEngine()
(c) class Fruits model stopEngine()
Registration Number
Characteristics Methods
name buy()
quantity sell()
cost

(d) class Pen


Characteristics Methods
name write()
type refill()
Company
Question 11
Design a program in Java to calculate the discount given to a customer on purchasing LED
Television. The program also displays the amount paid by the customer after discount. The details
are given as:
Class name : Television
Data members: int cost, int discount, int amount
Member functions:
Accept( ) : to input the cost of Television
Calculate( ) : to calculate the discount
Display( ) : to show the discount and the amount paid after discount
Answer
class Television {
int cost;
int discount;
int amount;

void Accept() {
/*
* Input the cost
* of Television
*/
}

void Calculate() {
/*
* Calculate the discount
*/
}

void Display() {
/*
* Show the discount and
* the amount paid
* after discount
Page 8 of 36
*/
}
}
Chapter 1 - Unit 3
Values and Data Types
State True or False
Question 1
There are 128 set of different characters used in a Java program.
False
Explanation — Java uses Unicode which can represent much more than 128 characters

Question 2
The ASCII codes of upper case letters range from 97 to 122.
False
Explanation — ASCII codes of upper case letters range from 65 to 90.

Question 3
A variable gives the exact representation of data.
False
Explanation — A literal gives exact representation of data. As value of variable can change, so it
cannot give exact representation of data.

Question 4
The data types int, float, char are called non-primitive types.
False
Explanation — The data types int, float, char are called Primitive types.

Question 5
A String literal is assigned to a String variable.
True
Explanation — The data type of the variable that can hold a String literal should also be String.

Question 6
A character literal is always enclosed in double quotes.
False
Explanation — A character literal is enclosed in single quotes.

Question 7
String constant can be written by using a set of alphanumeric characters.
True
Explanation — A String literal or String constant is defined by enclosing a set of alphanumeric
characters within double quotes.

Question 8
An object is said to be a non-primitive data.
True
Explanation — Class is a non-primitive data type so Objects are non-primitive data.

Question 9

Page 9 of 36
The data type int stores fractional values.
False
Explanation — float or double data type stores fractional values.

Question 10
Boolean type data is used to test a condition and results in either true or false.
True
Explanation — Boolean data type can be either true or false.
EXTRA MAIN
Multiple Choice Questions
Question 1
How many bytes a char data type occupies in the memory?
1. 2
2. 8
3. 4
4. 16
Answer
2
Reason — A char data type occupies 2 bytes in the memory.

Question 2
Which of the following is a non-primitive data type?
1. int
2. double
3. char
4. String
Answer
String
Reason — String is a non-primitive data type.

Question 3
Which of the following is non-numeric data type?

1. boolean
2. int
3. float
4. double
Answer
boolean
Reason — boolean data type can store only two values - true or false.

Question 4
Which of the following ASCII code range is applicable for lowercase letters?

1. 65 - 90
2. 90 - 115
3. 97 - 122
4. 95 - 110
Answer

Page 10 of 36
97 - 122
Reason — 97 - 122 is the ASCII code range for lowercase letters.

Question 5
What is not an escape sequence?

1. \t
2. \\
3. \n
4. ||
Answer
||
Reason — || is the symbol for logical OR operator.

Write short answers


Question 1
What is meant by data type? Name two types of data type.
Answer
Data types are used to identify the type of data a memory location can hold and the associated
operations of handling it. Data Types are of two types:

1. Primitive Data Types


2. Reference or Composite Data Types

Question 2
Why is it necessary to define data type in Java programming?
Answer
Data types tells Java how much memory it should reserve for storing the data value. Data types also
help in preventing errors as the compiler can check and flag illegal operations at compile time itself.

Question 3
Define the following with an example:
(a) variable
(b) constant
(c) boolean data type
(d) coercion
(e) primitive data type
(f) non-primitive data type
Answer
(a) Variable — Variables are identifiers that are used to name a data that holds a value in the
memory. The value can change depending upon our requirements in the program. For Example,
int mathScore = 95;
(b) Constant — 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;
(c) Boolean Data Type — A boolean data type is used to store one of the two boolean values — true
or false. The size of boolean data type is 8 bits or 1 byte.
Example:
boolean bTest = false;

Page 11 of 36
(d) Coercion — In a mixed mode expression, when the data type of the result gets converted into the
higher most data type available in the expression without any intervention of the user, is known as
Implicit Type conversion or Coercion.
Example:
int a = 42;
long b = 50000;
long c = a + b;
Here, a is int, b is long so the result of a + b automatically gets converted into long and assigned to
variable c which is of long type.
(e) Primitive Data Type — Primitive data types are the basic or fundamental data types used to
declare a variable. Examples of primitive data types in Java are byte, short, int, long, float, double,
char and boolean.
(f) Non-Primitive Data Type — A non-primitive data type is one that is derived from primitive data
types. A number of primitive data types are used together to represent a non-primitive data type.
Examples of non-primitive data types in Java are Class and Array.
Question 4
What is a token? Name different types of tokens.
Answer
A token is defined as each individual component of Java program that carries some meaning and
takes active part in program execution. The different types of tokens in Java are:

1. Identifiers
2. Literals
3. Operators
4. Punctuators
5. Separators
6. Keywords

Question 5
Explain the term type casting.
Answer
Type casting is a data conversion in which the data type of the result in a mixed mode expression,
gets converted into a specific type as per user's choice. The choice of data type must be written
within braces ( ) before the expression.
For example,
int a; float b; char c;
char d = (char) (a + b * c);

Question 6
Assign the following to a variable with suitable data type.
(a) m = 22 / 7
(b) p = 1.4142135 (value of square root of 2)
(c) k = 0.00004545
(d) n = 24.50
Answer
(a) double m = (22.0 / 7.0);
(b) double p = 1.4142135;
(c) double k = 0.00004545;
(d) float n = 24.50f;
Question 7
Distinguish between:

Page 12 of 36
(a) Token and Identifier (b) Character and Boolean literal
Answer
(a) Difference between Token and Identifier:
Token Identifier
A token is the smallest element of a program Identifiers are used to name things like classes,
that is meaningful to the compiler. objects, variables, arrays, functions an so on.
(b) Difference between Character and Boolean literal:
Character literal Boolean literal
A boolean literal can take only
Character literals are written by
one of the two boolean values
enclosing a character within a pair of
represented by the words true or
single quotes.
false.
Character literals can be assigned to
Boolean literals can only be
variables of any numeric data type
assigned to variables declared as
— byte, short, int, long, float,
boolean
double, char
Escape Sequences can be used to Only true and false values are
write character literals allowed for boolean literals
Question 8
Explain the term type conversion. How is implicit conversion different from explicit conversion?
Answer
The process of converting one predefined type into another is called type conversion.
In an implicit 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. For example:
int a = 10;
float b = 25.5f, c;
c = a + b;
In case of 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);
Question 9
Classify the following as primitive or non-primitive data types.
(a) char
(b) arrays
(c) int
(d) classes
Answer
(a) char — Primitive Data Type
(b) arrays — Non-Primitive Data Type
(c) int — Primitive Data Type
(d) classes — Non-Primitive Data Type
Question 10
In what way is static initialization of data type different from dynamic initialization?
Answer
In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example:
int mathScore = 100;
Page 13 of 36
double p = 1.4142135;
char ch = 'A';
In dynamic initialization, the initial value of the variable is the result of an expression or the return
value of a method call. Dynamic initialization happens at runtime. For example:
int a = 4;
int b = Math.sqrt(a);

double x = 3.14159, y = 1.4142135;


double z = x + y;
Question 11
Predict the return data type of 'r' and 'n' from the snippet:
int p; float m;
r = p+m;
n = m/3*(Math.pow(4,3));
System.out.println(r);
System.out.println(n);
Answer
Return type of r is float and n is double.
Question 12
Give reason whether the following assignments are correct or not.
(a) int m =155;
(b) float f = 0.002654132;
(c) String str = 'Computer';
(d) boolean p = false;
(e) String b = "true";
(f) char ch = "apps";
(g) String st= "Application";
(h) double n = 455.29044125;
Answer
(a) This assignment is correct as 155 is an integer literal and it is assigned to an int variable m.
(b) This assignment is incorrect as data type of 0.002654132 is double but it is assigned to a float
variable. The correct assignment will be double f = 0.002654132d;
(c) This assignment is incorrect as the String literal Computer is enclosed in single quotes. It should
be in double quotes. The correct assignment will be: String str = "Computer";
(d) This assignment is correct as false is a valid boolean literal and it is assigned to a boolean
variable.
(e) This assignment is correct as "true" is a string literal not a boolean literal as it is enclosed in
double quotes. It is correctly assigned to a String variable.
(f) This assignment is incorrect as "apps" is a string literal not a character literal and it is assigned to
a variable ch of char data type.
(g) This assignment is correct as "Application" is a string literal and it is correctly assigned to a
String variable.
(h) This assignment is correct as 455.29044125 is a literal of double data type and it is correctly
assigned to a double variable.

Chapter 1 - Unit 4
Operators in Java
State True or False
Question 1
The precedence of operators in Java follows BODMAS.
False
Question 2

Page 14 of 36
The output of a++ will be 1, if int a = -1.
False
Question 3
The relational operators always result in terms of 'True' or 'False'.
True
Question 4
Given: int m=5; m*=5 then the value stored in m results in 55.
False
Question 5
The statement (a>b)&&(a>c) uses a logical operator.
True
Question 6
If int a=27,b=4,c=0; then c = a % b; results in 3.
True
Question 7
The statement p += 5 means p = p*5.
False
Question 8
In the precedence of logical operators; NOT is followed by AND.
True

EXTRA MAIN
Multiple Choice Questions
Question 1
Which of the following is the correct precedence of logical operators?
1. !, &&, ||
2. &&, !, ||
3. ||, !, &&
4. &&, ||, !
Answer
!, &&, ||
Reason — The correct precedence of logical operators is !, &&, ||.
Question 2
Choose the odd one out from the following:
1. >
2. ==
3. &&
4. <
Answer
&&
Reason — && is the logical AND whereas the other operators are relational.
Question 3
Given: String st = (a>= 90)? "Excellent": "Best";
Predict the output, when a = 90.
1. Best
2. Excellent: Best
3. Best: Excellent
4. Excellent
Answer
Excellent
Reason — Since condition (a>= 90) of ternary operator is true, st is assigned the value of expression
1 i.e., excellent.
Question 4
Page 15 of 36
Choose the odd one out from the following:
1. Unary operator
2. Binary operator
3. Ternary operator
4. Bitwise operator
Answer
Bitwise operator
Reason — Unary operator, Binary operator and ternary operator are arithmetic operators.
Question 5
Given: x += x++ + ++x + --x
Find the value, when x=5.
1. 23
2. 21
3. 22
4. 24
Answer
23
Reason — The expression is calculated as follows:
x = x + (x++ + ++x + --x) (x = 5)
x = 5 + (5 + ++x + --x) (x = 6)
x = 5 + (5 + 7 + --x) (x = 7)
x = 5 + (5 + 7 + 6) (x = 6)
x = 5 + 18
x = 23
Write Java expressions
Answers: Page 37, II Main
1. z= 5 *x*x*x =2*y*x+y 6. p= a*a + b*b +2*a*b

2. m= a*a + b*b / (a +b) 7. y= 2(l*b + b*h + l*h)

3. s= u*t + 1.0/2.0 (a*t*t) 8. p= a/b*b + b/a*a

4. f = u*v /(u +v) 9. z= x*x*x + y*y*y – (y/z*z*z)

5. d= Math.sqrt(3*x+ x*x)/ a +b 10. q= 1/Math.sqrt(a +b) + 3/c*c

Predict the output


Question 1
int c = (3<4)? 3*4 : 3+4;
Output
12
Explanation
As 3 is less than 4 so condition of ternary operator is true. Variable c is assigned the value of
expression 1 which is 3 * 4 = 12.
Question 2
int a = 14, b = 4; boolean x = (a > b) ? true : false;
Output
true
Explanation
As 14 is greater than 4 so condition of ternary operator is true. Variable x is assigned the value of
expression 1 which is true.
Question 3

Page 16 of 36
int x = 90;
char c = (x<=90)? 'Z' : 'I';
Output
Z
Explanation
As value of x is 90 so condition of ternary operator is true. Variable c is assigned the value of
expression 1 which is Z.
Question 4
int a = 18; int b = 12;
boolean t = (a > 20 && b < 15)? true : false;
Output
false
Explanation
The condition a > 20 is false as value of a is 18. So the logical AND operator — && returns false.
Variable t is assigned the value of expression 2 which is false.
Question 5
c = (val + 550 < 1700)? 200: 400;
if: (a) val = 1000 (b) val = 1500
Output
(a) 200
(b) 400
Explanation
When val = 1000, val + 550 = 1550. As 1550 is less than 1700 so condition of ternary operator is
true. Variable c is assigned the value of expression 1 which is 200.
When val = 1500, val + 550 = 2050. As 2050 is greater than 1700 so condition of ternary operator is
false. Variable c is assigned the value of expression 2 which is 400.
Answer the following questions
Question 1
What is an operator? What are the three main types of operators? Name them.
Answer
An operator is a symbol or sign used to specify an operation to be performed in Java programming.
The three main types of operators are Arithmetical, Logical and Relational.
Question 2
How is Java expression different from statement?
Answer
An expression is a set of variables, constants and operators i.e. an expression is a combination of
operators and operands. When an expression is assigned to a variable, the complete set is referred to
as a statement.
Question 3
Explain the following with one example each.
(a) Arithmetic operator
Answer
Arithmetic operators are used to perform mathematical operations on its operands. Operands of
arithmetic operators must be of numeric type. A few arithmetic operators operate upon one operand.
They are called Unary Arithmetic operators. Other arithmetic operators operate upon two operands.
They are called Binary Arithmetic operators.
As an example consider the below statement:
int a = 10 + 20;
Here, the addition arithmetic operator, represented by the symbol + will add 10 and 20. So
variable a will be 30.
(b) Relational operator
Answer

Page 17 of 36
Relational operators are used to determine the relationship between the operands. Relational
operators compare their operands to check if the operands are equal to ( == ), not equal to ( != ), less
than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each other. The
result of an operation involving relation operators is a boolean value — true or false.
Example:
int a = 8;
int b = 10;
boolean c = a < b;
Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.
(c) Logical operator
Answer
Logical operators operate on boolean expressions to combine the results of these boolean expression
into a single boolean value.
Example:
int a = 7;
int b = 10;
boolean c = a < b && a % 2 == 0;
Here, the result of first boolean expression a < b is true and the result of second boolean expression a
% 2 is false. The logical AND operator ( && ) combines these true and false boolean values and
gives a resultant boolean value as false. So, boolean variable c becomes false.
(d) Ternary operator
Answer
Ternary operator operates on three operands. Its syntax is:
condition ? expression 1 : expression 2
Ternary operator evaluates the condition. If the condition is true then result of ternary operator is the
value of expression 1. Otherwise the result is the value of expression 2.
Example:
boolean isLeapYear = true;
int febDays = isLeapYear ? 29 : 28;
Here, the ternary operator checks if the value of boolean variable isLeapYear is true or false. As it is
true, expression 1, which in this example is the value 29, is the result of the ternary operator. So, int
variable febDays becomes 29.
Question 4
Distinguish between:
(a) Unary & Binary arithmetic operator
Answer
Unary Arithmetic Operator Binary Arithmetic Operator
It operates on a single operand It operates on two operands
Increment (++) and Decrement (--) operators are Multiplication (*) and Division (/) are
examples of Unary Arithmetic Operators examples of Binary Arithmetic Operators
(b) Postfix increment and Prefix increment
Answer
Postfix Increment Prefix Increment
It works on the principle of CHANGE-
It works on the principle of USE-THEN-CHANGE.
THEN-USE.
The increment operator (++) is written after the The increment operator (++) is written
operand. before the operand.

Page 18 of 36
Postfix Increment Prefix Increment
Example: Example:
int a = 99; int a = 99;
int b = a++; int b = ++a;
After the execution of these two statements, a will After the execution of these two statements,
have the value of 100 and b will have the value of 99. both a and b will have the value of 100.
(c) Postfix decrement and Prefix decrement
Answer
Postfix Decrement Prefix Decrement
It works on the principle of CHANGE-
It works on the principle of USE-THEN-CHANGE.
THEN-USE.
The decrement operator (--) is written after the The decrement operator (--) is written
operand. before the operand.
Example: Example:
int a = 100; int a = 100;
int b = a--; int b = --a;
After the execution of these two statements, a will After the execution of these two statements,
have the value of 99 and b will have the value of 100. both a and b will have the value of 99.
(d) (p != q) and !(p == q)
Answer
(p != q) !(p == q)
This expression uses the relational This expression first checks if values of p and q are equal using
operator != (Not equal to) to the relational operator == (equality). It then inverts the result of
determine if values of p and q are equality operator using the logical NOT (!) operator to
different. determine if values of p and q are different.
Question 5
What is the difference between
(a) / and % operator?
Answer
/ %
Division operator Modulus operator
Returns the quotient of division operation Returns the remainder of division operation
Example: int a = 5 / 2; Here a will get the value Example: int b = 5 % 2; Here b will get the value
of 2 which is the quotient of this division of 1 which is the remainder of this division
operation operation
(b) = and == ?
Answer
= ==
It is the assignment operator used for It is the equality operator used to check if a variable is
assigning a value to a variable. equal to another variable or literal.

Page 19 of 36
= ==
Example:
Example:
if (a == 10)
int a = 10;
This statement checks if variable a is equal to 10 or
This statement assigns 10 to variable a.
not.
Question 6(a)
What will be the output of the following code?
int k=5,j=9;
k += k++ - ++j + k;
System.out.println("k="+k);
System.out.println("j="+j);
Output
k=6
j=10
Explanation
k+= k++ - ++j + k
⇒ k = k + (k++ - ++j + k)
⇒ k = 5 + (5 - 10 + 6)
⇒k=5+1
⇒k=6
Question 6(b)
If int y =10 then find int z = (++y*(y++ + 5));
Output
z = 176
Explanation
z = (++y * (y++ + 5))
⇒ z = (11 * (11 + 5))
⇒ z = (11 * 16)
⇒ z = 176
Question 6(c)
Give the output of the following expression:
a += a++ + ++a + --a + a--; when a = 7;
Output
a = 39
Explanation
a+= a++ + ++a + --a + a--
⇒ a = a + (a++ + ++a + --a + a--)
⇒ a = 7 + (7 + 9 + 8 + 8)
⇒ a = 7 + 32
⇒ a = 39
Question 6(d)
What is the value of y after the execution?
y += ++y + y-- + --y; when int y=8
Output
y = 33
Explanation
y+= ++y + y-- + --y
⇒ y = y + (++y + y-- + --y)
⇒ y = 8 + (9 + 9 + 7)
⇒ y = 8 + 25
⇒ y = 33

Page 20 of 36
Question 7
Rewrite the following program segment using if-else statements instead of the ternary operator.
(a) String grade = (marks>=90)?"A": (marks>=80)? "B": "C";
Answer
String grade;
if (marks >= 90)
grade = "A";
else if (marks >= 80)
grade = "B";
else
grade = "C";
(b) commission = (sale > 5000) ? sale*10/100 : 0;
Answer
if (sale > 5000)
commission = sale * 10 / 100;
else
commission = 0;
(c) net = (salary > 10000) ? salary - (8.33/100)*salary : salary - (5/100)*salary
Answer
if (salary > 10000)
net = salary - (8.33/100) * salary;
else
net = salary - (5/100) * salary;
(d) s = (a + b < c || a + c <= b || b + c <= a) ? "Triangle is not possible": "Triangle is possible";
Answer
if (a + b < c || a + c <= b || b + c <= a)
s = "Triangle is not possible";
else
s = "Triangle is possible";
(e) c = (x >= 'A' && x <= 'Z') ? "Upper Case Letter" : "Lower Case Letter";
Answer
if (x >= 'A' && x <= 'Z')
c = "Upper Case Letter";
else
c = "Lower Case Letter";
Question 8
Rewrite the following using ternary operator.
(a)
if (x % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
Answer
System.out.println(x % 2 == 0 ? "Even" : "Odd");
(b)
if (bill > 10000)
discount=bill*10.0/100;
else
discount=bill*5.0/100;
Answer
discount = bill > 10000 ? bill*10.0/100 : bill*5.0/100;
(c)
if(income < 10000)

Page 21 of 36
tax = 0;
else
tax = 12;
Answer
tax = income < 10000 ? 0 : 12;
(d)
if(a > b)
{
if (a > c)
g = a;
else
g = c;
}
else if (b > c)
g = b;
else
g = c;
Answer
g = a > b ? a > c ? a : c : b > c ? b : c;
(e)
if (p >= 4750)
k = p * 5 / 100;
else
k = p * 10 / 100;
Answer
k = (p >= 4750) ? p * 5 / 100 : p * 10 / 100;
Chapter 1 - Unit 5a
Introduction to Java
Multiple Choice Questions
Question 1
A Java program developed and executed by the users without using web browser is known as:
1. application
2. applet
3. object
4. none
Answer
application
Reason — Java application is a Java program that is developed to run on a computer without any
help of a web browser.
Question 2
Who developed Java?
1. James Gosling
2. Robert James
3. Bjarne Stroustrup
4. None
Answer
James Gosling
Reason — James Gosling developed Java.
Question 3
Java Virtual Machine (JVM) is an:
1. interpreter
2. compiler
3. machine code
Page 22 of 36
4. byte code
Answer
interpreter
Reason — Java Virtual Machine (JVM) is a Java interpreter.
Question 4
Java is a case sensitive language. Which is the most appropriate statement with respect to this
context?
1. Upper and lower case letters are distinguished.
2. Upper and lower case letters are ignored.
3. Only lower case letters are distinguished.
4. None.
Answer
Upper and lower case letters are distinguished.
Reason — Java is case sensitive, i.e., it can distinguish between uppercase and lowercase letters.
Question 5
Which of the following package is needed to find the square root of a number?
1. java.text
2. java.math
3. java.lang
4. None
Answer
java.lang
Reason — Math class inside java.lang package contains mathematical functions. The official
documentation is provided here. It should not be confused with java.math package which contains
BigDecimal, BigInteger and MathContext classes.
Question 6
Which of the following is not a Java reserved word?
1. private
2. public
3. break
4. total
Answer
total
Reason — total is not a Java reserved word.
Question 7
The most suitable statement for BlueJ is:
1. It is a window based platform.
2. It is a DOS based platform.
3. It is a window based and DOS based platform.
4. None.
Answer
It is a window based and DOS based platform.
Reason — BlueJ is a window based and DOS based platform.
Fill in the blanks
Question 1
The initial name of Java was Oak.
Question 2
Compiler converts source code to byte code.
Question 3
JVM is used to convert Byte Code into machine code.
Question 4
Java is platform independent language.
Question 5
Page 23 of 36
Windows based Java platform is known as BlueJ.
Question 6
Reserved words are also called keywords.
Question 7
java.lang package is used for mathematical functions in Java.
Clarification: Math class inside java.lang package contains mathematical functions. The official
documentation is provided here. It should not be confused with java.math package which contains
BigDecimal, BigInteger and MathContext classes.
Predict the output of the following snippets
Question 1
System.out.println("My name is ");
System.out.print("Kunal Kishore");
System.out.println("I am a student of Class X");
Output
My name is
Kunal KishoreI am a student of Class X
Question 2
System.out.println("This is my first Java program");
System.out.print("Display the message:");
System.out.print("Have fun!!!");
Output
This is my first Java program
Display the message:Have fun!!!
Question 3
System.out.println("India got independence");
System.out.println("on");
System.out.println("15th August,1947");
Output
India got independence
on
15th August,1947
Question 4
System.out.print("The factorial of 5 is");
System.out.println("120");
Output
The factorial of 5 is120
Write short answers
Question 1
Name a Java package which is imported by default.
Answer
java.lang is imported by default in Java.
Question 2
Explain the significance of the following Java library packages.
(a) java.awt
(b) java.io
(c) java.net
Answer
(a) java.awt — java.awt package contains classes for supporting abstract window tool kit and
managing Graphical User Interface (GUI) components.
(b) java.io — It contains the classes that handle fundamental input and output operations in Java.
(c) java.net — It contains classes for supporting network operations in Java.
Question 3(a)
Java interpreter is called Java Virtual machine. Explain.
Page 24 of 36
Answer
Java interpreter enables a computer to execute a Java program as well as a program written in other
language compiled into Java byte code. Hence, java interpreter is called Java Virtual machine.
Question 3(b)
Name two ways of writing Java programs.
Answer
Two ways of writing Java programs are:
1. Java Application
2. Java Applet
Question 4
Define the terms:
(a) Source code
(b) Machine code
(c) Byte code
Answer
(a) Source code — A set of statements written in a High-Level programming language like Java,
C++, Python, etc. is called as Source Code.
(b) Machine code — Machine code is a low-level programming language. Instructions in machine
code are written as a sequence of 0s and 1s. It is directly understood and executed by the processor.
(c) Byte code — Java compiler converts Java source code into an intermediate binary code called
Byte code. Byte code can't be executed directly on the processor. It needs to be converted into
Machine Code first.
Question 5
What is BlueJ? What are the features of BlueJ?
Answer
BlueJ is an integrated development environment for Java. It was created for teaching Object Oriented
programming to computer science students. Features of BlueJ include a simple beginner friendly
graphical user interface to develop Java programs. It allows creating objects of the class dynamically,
invoking their methods and also supplying data to the method arguments if present.
Question 6
Write down the syntax of output statement in Java with an example.
Answer
We commonly use two output statements in Java. Their syntax is as follows:
1. System.out.println(<output value>);
This statement displays the output of the program on the screen in different lines. The letters
'ln' in the statement act as a line feed which directs the cursor to move to the next line.
2. System.out.print(<output value>);
This statement is used to display the value enclosed within its braces. It leaves the cursor in
the same line on the screen.
As an example, the below statements will generate the following output:
System.out.println("JVM stands for");
System.out.print("Java ");
System.out.print("Virtual ");
System.out.print("Machine");
Output
JVM stands for
Java Virtual Machine
Question 7
What is meant by Java reserved words? Name five Java reserved words which are commonly used in
Java programming.
Answer
There are some words that carry special meaning to the system compiler. These words can't be used
as variable names in the program. Such words are called Keywords or reserved words.

Page 25 of 36
Five commonly used Java reserved words are:
1. public
2. class
3. int
4. double
5. char
Question 8
Differentiate between the output statements System.out.println() and System.out.print().
Answer
System.out.println() System.out.print()
It prints data to the console and places the It prints data to the console but the cursor remains at
cursor in the next line. the end of the data in the same line.
Next printing takes place from next line. Next printing takes place from the same line.
Question 9
A Java program uses a Compiler as well as an Interpreter. Explain.
Answer
Java compiler compiles Java source code to Byte code. Byte code cannot run on the processor
directly as processor only understands machine code. Java Virtual Machine (JVM) takes this byte
code as input and converts it into machine code line by line. So, JVM acts as an interpreter for
converting byte code to machine code. In this way, a Java program uses both a compiler as well as an
interpreter to get executed on the processor.
Question 10
Write a package that is used for input/output operation in Java
Answer
java.io package is used for input/output operation in Java.

Chapter 1 - Unit 5b
Input in Java
Multiple Choice Questions
Question 1
Which of the following is the right way to use a comment in Java program?

1. /* comment */
2. /* comment
3. // comment //
4. */ comment */
Answer
/* comment */
Reason — /* comment */ is used to write multi line comments.

Question 2
Given: int p=55/0;
Name the type of error in the given statement:

1. Syntax
2. Logical
3. Runtime
4. Executional
Answer
Page 26 of 36
Runtime
Reason — Division by 0 will result in an infinite value, which is a runtime error.

Question 3
The ............... method will accept a character using stream class.

1. in.read( )
2. (char)(in.read( ))
3. in.readline( )
4. character.read( )
Answer
(char)(in.read( ))
Reason — (char)(in.read( )) method will accept a character using stream class.

Question 4
Which of the following method will accept a string by using scanner object?

1. next( )
2. next.read( )
3. next.String( )
4. next.char( )
Answer
next( )
Reason — next( ) method will accept a string by using scanner object.

Question 5
Which of the following is a method of Java Scanner class?

1. nextline( )
2. nextLine( )
3. Nextline( )
4. NextLine( )
Answer
nextLine( )
Reason — nextLine( ) is a method of Java Scanner class.
Fill in the blanks

Question 1
The package needed to import StreamReader class is java.io.

Question 2
nextInt() function accepts an integer by using scanner object.

Question 3
The package needed to import Scanner class is java.util.

Question 4
The keyword import is used to include a package in a program.

Page 27 of 36
Question 5
Division by a variable that contains a value zero is called syntax error.

Question 6
input statement is used to accept value at run time.

Question 7
N=next() function accepts a string by using scanner object.

Question 8
void keyword refers to a non-returnable type function.

Question 9
Scanner ob = new Scanner(System.in).

Question 10
The command line argument accepts the data value as an array of Strings.
Explain the following functions

Question 1
Integer.parseInt(in.readLine());
Answer
This statement accepts an integer from the user using Stream class and parses it into int data type
using Integer.parseInt() method.

Question 2
(char)(in.read());
Answer
This statement reads a character using Stream class and casts it explicitly into char data type.

Question 3
next();
Answer
next() method accepts a string from the user as a word using Scanner class.

Question 4
public static void main(int b)
Answer
The given function accepts an integer value from the user at the time of execution of the program.
The value for variable b must be provided as arguments to the main() function.

Question 5
nextLine();
Answer
nextLine() method accepts a string from the user as a line of text using Scanner class.
Answer the following

Question 1

Page 28 of 36
What are the different ways to give input in a Java Program?
Answer
Java provides the following ways to give input in a program:

1. Using Function Argument


2. Using InputStreamReader class
3. Using Scanner class
4. Using Command Line Arguments

Question 2
What is a package? Give an example.
Answer
In Java, a package is used to group related classes. Packages are of 2 types:

1. Built-In packages — These are provided by Java API


2. User-Defined packages — These are created by the programmers to efficiently structure their code.
For example, java.util, java.lang are built-in packages.

Question 3
What is the use of the keyword 'import' in Java programming?
Answer
'import' keyword is used to import built-in and user-defined packages into our Java programs.

Question 4
What is a Runtime error? Explain with an example.
Answer
Errors that occur during the execution of the program primarily due to the state of the program which
can only be resolved at runtime are called Runtime errors.
Consider the below example:
import java.util.Scanner;
class RunTimeError
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all non-zero values of n entered by the user. When the user enters
zero, a run-time error will occur as the program is trying to perform an illegal mathematical
operation of division by 0. When we are compiling the program, we cannot say if division by 0 error
will occur or not. It entirely depends on the state of the program at run-time.

Question 5
What are the different types of errors that take place during the execution of a program? Name them.
Answer
Logical errors and Run-Time errors occur during the execution of the program.

Question 6
Page 29 of 36
Give two differences between Syntax error and Logical error.
Answer
Syntax error Logical error

Syntax errors occur when we violate the rules of writing Logical errors occur due to our mistakes in
the statements of the programming language. programming logic.

Program compiles and executes but doesn't


Program fails to compile and execute.
give the desired output.

Logical errors need to be found and corrected


Syntax errors are caught by the compiler.
by people working on the program.

Solutions to Unsolved Java Programs

Question 1
In an election, there are two candidates X and Y. On the election day, 80% of the voters go for
polling, out of which 60% vote for X. Write a program to take the number of voters as input and
calculate:

1. number of votes received by X


2. number of votes received by Y
import java.util.Scanner;

public class Polling


{
public static void main(String args[])
{

Scanner in = new Scanner(System.in);

System.out.print("Enter the number of voters: ");


long totalVoters = in.nextLong();

long votedVoters = Math.round(0.8 * totalVoters);


long xVotes = Math.round(0.6 * votedVoters);
long yVotes = votedVoters - xVotes;

System.out.println("Total Votes: " + votedVoters);


System.out.println("Votes of X: " + xVotes);
System.out.println("Votes of Y: " + yVotes);
}
}

Page 30 of 36
Output

Question 2
A shopkeeper offers 10% discount on the printed price of a mobile phone. However, a customer has
to pay 9% GST on the remaining amount. Write a program in Java to calculate the amount to be paid
by the customer taking printed price as an input.
import java.util.Scanner;

public class Invoice


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the MRP: ");


double mrp = in.nextDouble();

double priceAfterDiscount = mrp - (0.1 * mrp);


double priceWithGST = priceAfterDiscount + (priceAfterDiscount *
0.09);

System.out.println("Price after 10% discount and 9% GST: " +


priceWithGST);
}
}

Output

Question 3
A man spends (1/2) of his salary on food, (1/15) on rent, (1/10) on miscellaneous activities. Rest of
the salary is his saving. Write a program to calculate and display the following:

Page 31 of 36
1. money spent on food
2. money spent on rent
3. money spent on miscellaneous activities
4. money saved
Take the salary as an input.

import java.util.Scanner;

public class Salary


{
public static void main(String args[])
{

Scanner in = new Scanner(System.in);

System.out.print("Enter the Salary: ");


float salary = in.nextFloat();

float foodSpend = salary / 2;


float rentSpend = salary / 15;
float miscSpend = salary / 10;
float savings = salary - (foodSpend + rentSpend + miscSpend);

System.out.println("Money spent on food: " + foodSpend);


System.out.println("Money spent on rent: " + rentSpend);
System.out.println("Money spent on miscellaneous: " + miscSpend);
System.out.println("Money saved: " + savings);
}
}

Output

Question 4
Write a program to input time in seconds. Display the time after converting them into hours, minutes
and seconds.
Sample Input: Time in seconds: 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
import java.util.Scanner;

public class Time


{
public static void main(String args[])
{

Scanner in = new Scanner(System.in);


Page 32 of 36
System.out.print("Time in seconds: ");
int inputTime = in.nextInt();

int hrs = inputTime / 3600;


int mins = (inputTime % 3600) / 60;
int secs = (inputTime % 3600) % 60;

System.out.println(hrs
+ " Hours "
+ mins
+ " Minutes "
+ secs
+ " Seconds");
}
}

Output

Question 5
The driver took a drive to a town 240 km at a speed of 60 km/h. Later in the evening, he drove back
at 20 km/h less than the usual speed. Write a program to calculate:

1. the total time taken by the driver


2. the average speed during the whole journey
[Hint: average speed = total distance / total time]
public class Journey
{
public static void main(String args[])
{

int distance = 240;


float speed = 60.0f;
float returnSpeed = speed - 20;

float time2Reach = distance / speed;


float time2Return = distance / returnSpeed;
float totalTime = time2Reach + time2Return;

float avgSpeed = (distance * 2) / totalTime;

System.out.println("Total time: " + totalTime);


System.out.println("Average speed: " + avgSpeed);
}
}

Page 33 of 36
Output

Question 6
Write a program to input two unequal numbers. Display the numbers after swapping their values in
the variables without using a third variable.
Sample Input: a = 76, b = 65
Sample Output: a = 65, b = 76
import java.util.Scanner;

public class NumberSwap


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Please provide two unequal numbers");

System.out.print("Enter the first number: ");


int firstNum = in.nextInt();

System.out.print("Enter the second number: ");


int secondNum = in.nextInt();

if (firstNum == secondNum) {
System.out.println("Invalid Input. Numbers are equal.");
return;
}

firstNum = firstNum + secondNum;


secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;

System.out.println("First Number: " + firstNum);


System.out.println("Second Number: " + secondNum);
}
}

Output

Page 34 of 36
Question 7
A certain amount of money is invested for 3 years at the rate of 6%, 8% and 10% per annum
compounded annually. Write a program to calculate:

1. the amount after 3 years.


2. the compound interest after 3 years.
Accept certain amount of money (Principal) as an input.
Hint: A = P * (1 + (R1 / 100))T * (1 + (R2 / 100))T * (1 + (R3 / 100))T and CI = A - P
import java.util.Scanner;

public class CompoundInterest


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the principal: ");


double principal = in.nextDouble();
System.out.println(principal);

float r1 = 6.0f, r2 = 8.0f, r3 = 10.0f;

double amount = principal * (1 + (r1 / 100)) * (1 + (r2 / 100)) *


(1 + (r3 / 100));
double ci = amount - principal;

System.out.println("Amount after 3 years: " + amount);


System.out.println("Compound Interest: " + ci);
}
}

Output

Question 8
The co-ordinates of two points A and B on a straight line are given as (x1,y1) and (x2,y2). Write a
program to calculate the slope (m) of the line by using formula:
Slope = (y2 - y1) / (x2 - x1)
Take the co-ordinates (x1,y1) and (x2,y2) as input.
import java.util.Scanner;
Page 35 of 36
public class LineSlope
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter x coordinate of point A: ");


int x1 = in.nextInt();

System.out.print("Enter y coordinate of point A: ");


int y1 = in.nextInt();

System.out.print("Enter x coordinate of point B: ");


int x2 = in.nextInt();

System.out.print("Enter y coordinate of point B: ");


int y2 = in.nextInt();
float lineSlope = (y2 - y1) / (float)(x2 - x1);
System.out.println("Slope of line: " + lineSlope);
}
}

Output

Page 36 of 36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy