Copy of Class as the Basis of All Computation
Copy of Class as the Basis of All Computation
Question 1
Which keyword makes class members accessible outside the class in which they are declared?
1. Private
2. Protected
3. Public
4. Hidden
Question 2
Find the access specifier which prohibits a class member from being used outside a class:
1. Private
2. Public
3. Protected
4. None
Question 3
1. Identifier
2. Instance
3. Specifier
4. Modifier
Question 4
Which of the following statements is the most appropriate for the private members?
1. They are visible out of the class in which they are defined.
2. They can be used in the sub-classes.
3. They are only visible in the class in which they are declared.
4. None of the above.
Question 5
Which of the following keywords are used to control access to a class member?
1. Default
2. Abstraction
3. Protected
4. Interface
Question 6
1. Private
2. Public
3. Protected
4. All of the above
Question 7
1. 1
2. 2
3. On the user's choice
4. Number of variables
Question 8
A class contains:
Question 9
1. Encapsulation
2. Transparency
3. Inheritance
4. Polymorphism
Question 10
A package is a:
1. collection of data.
2. collection of functions.
3. collection of classes.
4. a nested class.
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
protected members are accessible in its own class as well as in a sub class.
Question 1
A composite data type is one which is composed with various primitive data types. A class
can contain various primitive data types as its data members so it is known as a composite
data type.
Question 2
Question 3
Question 4
Since a class is created by the user, it is also known as user defined data type.
Question 5
When a class is declared with public access specifier it is said that the class is publicly
accessible. A publicly accessible class is visible everywhere both within and outside its
package. For example:
Question 6
private members of a class are accessible only within the class in which they are declared.
public members of the class are accessible both within and outside their class.
Question 7
Two attributes required for class declaration are the keyword 'class' and the name of the
class.
Question 8
class Cuboid {
private double height;
private double width;
private double depth;
private double volume;
Question 9
private — A data member or member method declared as private is only accessible inside
the class in which it is declared.
public — A data member or member method declared as public is accessible inside as well
as outside of the class in which it is declared.
Question 10
A member method of a class declared with private access specifier is said to have private
visibility. Only other member methods of its class can call this method.
Question 1
How can class be used as user defined data types? Explain it with the help of an example.
Since a class is created by the user, it is also known as user defined data type. Take the
example of the below class:
class Cuboid {
private double height;
private double width;
private double depth;
private double volume;
public void input(int h, int w, int d) {
height = h;
width = w;
depth = d;
}
After defining this class, I can use Cuboid as a new data type and create variables of
Cuboid type as shown below:
Question 2
Differentiate between built-in data types and user defined data types.
Built-In Data Types are fundamental data types User Defined Data Types are created by
defined by Java language specification. the user.
Built-In Data Types are available in all parts of a Availability of User Defined data types
Java program. depends upon their scope.
Question 3
Question 4
The classes that contain public static void main(String args[]) method are not considered
as user defined data type. Only the classes that don't contain this method are called user
defined data type. The presence of public static void main(String args[]) method in a class,
converts it into a Java application so it is not considered as a user defined data type.
Question 5
They are declared using keyword 'static'. They are declared without using keyword 'static'.
All objects of a class share the same copy Each object of the class gets its own copy of Non-
of Static data members. Static data members.
They can be accessed using the class They can be accessed only through an object of
name or object. the class.
Question 6
Private members are only accessible inside the class in which they are defined and they
cannot be inherited by derived classes. Protected members are also only accessible inside
the class in which they are defined but they can be inherited by derived classes.
Question 7
They are declared without using keyword 'static'. They are declared using keyword 'static'.
Each object of the class gets its own copy of All objects of a class share the same copy
instance variables. of class variables.
They can be accessed only through an object of They can be accessed using the class name
the class. or object.
Question 1
Write a class program to accept two numbers as instance variables. Use the following functions
for the given purposes:
Class Name — Calculate
void inputdata() — to input both the values
void calculate() — to find sum and difference
void outputdata() — to print sum and difference of both the numbers
Use a main method to call the functions.
import java.util.Scanner;
Output
Question 2
import java.util.Scanner;
Output
Question 3
Calculate tax based on the given conditions and display the output as per the given format.
Up to ₹2,50,000 No tax
Output:
import java.util.Scanner;
Output
Question 4
void input() Stores the cost of the article and name of the customer
void display() Displays the name of the customer, cost, discount and amount to be paid
Write a program to compute the discount according to the given conditions and display the
output as per the given format.
Up to ₹5,000 No discount
Output:
import java.util.Scanner;
Output
Question 5
int prv, pre to store the previous and present meter readings
Member
Purpose
functions
Member
Purpose
functions
void input() Stores the previous reading, present reading and name of the consumer
Displays the name of the consumer, calls made, amount and total amount
void display()
to be paid
Write a program to compute the monthly bill to be paid according to the given conditions and
display the output as per the given format.
However, every consumer has to pay ₹180 per month as monthly rent for availing the service.
Output:
import java.util.Scanner;
Output
Question 6
Write a program to compute the interest according to the given conditions and display the
output.
import java.util.Scanner;
interest = (p * r * t) / 100.0;
amt = p + interest;
}
Output
Question 7
int day to store the number of days for which fine is to be paid
void input() To accept the name of the book and printed price of the book
void display() Displays the name of the book and fine to be paid
Write a program to compute the fine according to the given conditions and display the fine to
be paid.
Days Fine
import java.util.Scanner;
Output
Question 8
Up to 5 years 15%
import java.util.Scanner;
Output
Question 9
Hero Honda has increased the cost of its vehicles as per the type of the engine using the
following criteria:
Write a program by using a class to find the new cost as per the given specifications:
void find() To find the new cost as per the criteria given above
void printcost() To print the type and new cost of the vehicle
import java.util.Scanner;
default:
System.out.println("Incorrect type");
break;
}
}
Output
Question 10
int days To store the number of days the bike is taken on rent
Days Charge
Output:
import java.util.Scanner;
Output
Question 11
Member
Purpose
Methods
to obtain a string after converting each upper case letter into lower case
void convert()
and vice versa
Output
Question 12
import java.util.Scanner;
Output
Question 13
A bookseller maintains record of books belonging to the various publishers. He uses a class with
the specifications given below:
Member Methods:
1. void getdata() — To accept title, author, publisher's name and the number of copies.
2. void purchase(int t, String a, String p, int n) — To check the existence of the book in the
stock by comparing total, author's and publisher's name. Also check whether noc >n or
not. If yes, maintain the balance as noc-n, otherwise display book is not available or stock
is under flowing.
import java.util.Scanner;
Output
Question 14
Data Members:
Member Methods:
import java.util.Scanner;
public class Characters
{
private String str;
Output
Question 15
void display() To show all the details viz. name, marks, total and average
import java.util.Scanner;
Output
Question 16
int hours To store the number of hours the vehicle is parked in the parking lot
Member
Purpose
Methods
void calculate( To compute the parking charge at the rate ₹3 for the first hour or the part
) thereof and ₹1.50 for each additional hour or part thereof.
Member
Purpose
Methods
Write a main method to create an object of the class and call the above methods.
import java.util.Scanner;
Output
Question 17
void accept() to store the name of the customer and number of units consumed
void calculate() to calculate the bill as per the tariff table given below
Tariff Table
Number of units Rate per unit
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
Output:
Name of the customer: _________________________
Number of units consumed: _____________________
Bill amount: ________________
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
Question 18
int totalamt To store the amount to be paid after updating the original amount
Member
Purpose
Methods
Member
Purpose
Methods
void accept() To take input for name, coach, mobile number and amount
To update the amount as per the coach selected (extra amount to be added
void update()
in the amount as per the table below)
To display all details of a customer such as name, coach, total amount and
void display()
mobile number
First_AC ₹700
Second_AC ₹500
Third_AC ₹250
Sleeper None
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
Output