Chapter 5
Q3. What do you mean by an object and a class?
Ans:
- Object
- has identity, state and behaviour
- occupies a block of memory
- the block stores all instance variable along with their values
- creating an object means instantiating an object
- new operator is used to create an object
- Syntax for creating an object: <class-name> <object-name> = new <class-name>();
- Eg: Box b1 = new Box();
- Class
- represents a group of objects that share common properties.
- consists of data members + functions
- also known as blueprint for an object
- with the help of class objects are created
- it defines a data type
Q4. How do you explain the state of an object? Give two examples also.
Ans:
- state represents the data of an object
- For example:
- a Student object will have a first name, last name, age, etc.
- a Car object will have speed, rpm, gear, fuel level, engine temperature etc
Q5. Write a valid java statement to create an object for a class “Employee”.
Ans:
- Employee ramesh = new Employee();
Q6. What do you mean by members of a class?
Ans:
- Members of a class are of two types:
- Data
- represents the state of an object
- Functions
- represents the behaviour of an object
Q7. State the difference between a static variable and instance variable
Ans:
Static Variable |
Instance Variable |
Declared only once |
Declared as many times as the no. of objects |
Memory is allocated for it at class loading |
Memory is allocated when an object is created |
Can be accessed directly using class name |
Can be accessed using object name |
only one copy is there for all objects |
separate copies for each object |
Q8. What do you mean by a method or function?
Ans:
- a collection of statements or commands which are used to run a particular task.
- Types - User Defined & Standard Library Functions
- Syntax :
<return-type> <function-name> (<parameter-List>)
{
//Function Body
} - Function can return only one value.
- Return type can be primitive or non-primitive
- Parameter list is optional
- The function is executed only when it is called.
- return keyword causes the control to go back to the calling statement
Q9. Define method or function prototype with an example.
Ans:
- Function prototype is also known as function declaration.
- First line of a function is known as function prototype.
- For
eg:
public void max(int a, int b) //Line1
{
} - In the above example Line 1 is the function prototype.
Q10. What do you mean by actual parameters and formal parameters?
Ans:
- Actual parameters are situated in caller function
- Formal Parameters are situated in called function
- For ex:
class test
{
void main()
{
sum(5,6); //Line 1
}
void sum(int a, int b) //Line 2
{
int c = a + b;
}
} - In the above example 5 and 6 are actual parameters while a and b are Formal Parameters
Q11. Define the purpose of the return keyword with an example.
Ans:
- class
test
{
void main()
{
int add = sum(5,6); //Line 1
}
int
sum(int a, int b) //Line
2
{
int
c = a +
b;
return
c; //Line2
}
}
- return keyword causes the control to go back to the calling statement.
- In the above example Line 1 is the calling
statement. When
this is executed the control
is transfered to sum function - Inside the sum function when return keyword is executed the control goes back to Line1
Q12. Write the prototype of a method show() with one floating parameter and returns an integer.
Ans: int
show(float n)
Q13. Write the prototype of a method compute() with one floating parameter, one double parameter and returns a double type value.
Ans: double compute( float a, double b)
Q14. Write the prototype of a method Sum() with two integer parameters and no return type.
Ans: void Sum(int a, int b)
Q15. Write the prototype of a method change() with two character parameters and returns a character.
Ans: char
change(char a, char b)
Q16. Write the prototype of a method sum() with one integer parameter, one floating parameter and returns an integer.
Ans: int sum(int a, float b)
Q17. Write the prototype of a method display() with one long parameter, one integer parameter and one floating parameter and returns a long integer.
Ans: long display(long a, int b, float c)
Q18. Define a function cube() in a class Sample, which takes an int-type argument, calculate the cube, and returns the result.
Soln:
class Sample
{
int cube(int side)
{
int cube = side * side * side;
return (cube);
}
}
Q19. Create a class Tester with two methods Prod(), it takes two integer arguments x,y and returns the product of x,y and another method result() to print the product of 8, 2 and 10, 24 by invoking or calling method Prod() two times.
Soln:
class tester
{
int Prodt(int x, int y)
{
return (x * y);
}
void display()
{
System.out.println( Prodt(8, 2) );
System.out.println( Prodt(10, 24) );
}
}
Q20. Write a program to define a method void changer(), to initialise a character in uppercase form and convert into its lowercase form. Print the original character and new character.
Soln:
void changer()
{
char uCase = ‘A’;
char lCase = (char) (uc + 32);
System.out.println(“Uppercase Letter: “ + uCase);
System.out.println(“Lowercase Letter: “ + lCase);
}
Q21. Write a program to define a class Numbers with a method void change(int x, int y), to interchange or swap the argument values in x and y. Print the numbers before and after swap.
Soln:
class Numbers
{
void change(int x, int y)
{
System.out.println(“Before swap: x = “ + x);
System.out.println(“Before swap: y = “ + y);
int temp = x;
x = y;
y = temp;
System.out.println(“After swap: x = “ + x);
System.out.println(“After swap: y = “ + y);
}
}
Q22. Write a program to define a method void change(), to initialise a character in lowercase form and convert into its opposite case. Print the original character and the new character.
Soln: Same as Q20
Q23. Write a program to define a class Convert with a method void change(int x, int y), to interchange
or swap the argument values in x and y without using third or any extra variable. Print the numbers before and after interchange.
Soln:
class Convert
{
void change(int x, int y)
{
System.out.println(“Before change x and y = “ + x + “ and “ + y);
x = x + y;
y = x - y;
x = x - y;
System.out.println(“Before change x and y = “ + x + “ and “ + y);
}
}
Q24. Define a method Swap() in a class change, which takes two character type arguments ch1 and ch2. Interchange the values of ch1 and ch2. Print the values of ch1 and ch2 before and after interchange.
Soln:
class change
{
void Swap(char ch1, char ch2)
{
System.out.println("Values of ch1 and ch2 before interchange: " + ch1 + " and " + ch2);
char temp = ch1;
ch1 = ch2;
ch2 = temp;
System.out.println("Values of ch1 and ch2 after interchange: " + ch1 + " and " + ch2);
}
}
Q25. Write a program to define a method digits() with an integer argument to print the last two digits and the last three digits of the number in the argument. Take the actual value of integer argument more than 4 digits while defining the function.
Soln:
public class digits1
{
void digits(int num)
{
System.out.println("Given number = " + num);
System.out.println("Last two digits: " + num % 100 );
System.out.println("Last three digits: " + num % 1000);
}
}
Q26. Write a program to assign (or initialise) a capital letter between A-X. Print the original character and next two characters.
Soln:
public class assign1
{
static void assign(char ch1)
{
System.out.println("Original Character: " + ch1);
char ch2 = ++ch1;
char ch3 = ++ch1;
System.out.println("Next Two Characters: " + ch2 + ", " + ch3);
}
}
Q27. Write a program to define a method product() with an integer argument to return the product of last two digits of the number argument
Ans:
public class pro
{
int prod(int num)
{
System.out.println("Given number: " + num);
int d1, d2;
d1= num % 10;
num = num / 10;
d2 = num % 10;
return (d1 * d2);
}
}
Q28. Write a program to define two methods as follows:
- void Square(float s) - to print the area of a square whose side is given by s.
- float Rectangle(float l, float b) - to return the area of rectangle using l and b.
Soln:
public class area
{
static void Square(float s)
{
System.out.println("Side: " + s);
System.out.println("Area: " + s * s);
}
static float Rectangle(float l, float b)
{
System.out.println("Length: " + l + " Breadth = " + b);
return l * b;
}
}
Q29. Write a program to print the area of a circle using the formula: area = π r2
Ans:
class areac
{
void area(double pi)
{
System.out.println(“Area of circle: “ + 22 / 7 * r * r)
}
}
Q30. Identify the errors in the following statements and also write its correct forms:
(i) Int ne w = 347.0; → int new = 347;
(ii) chars var$ = M; → char var$ = ‘M’ ;
(iii) Double jk = = 87. → Double jk = 87;
(iv) void public print(int y, float d) → public void print(int y, float d)
(v) void Demo(float y, d, Long ch, Char h) → void Demo(float y, float d, Long ch, Char h)
Q31. Write a program to input employee code number, monthly basic pay(in decimals) of a person.
Calculate the following:
- 65% of monthly basic pay as DA.
- 21% of monthly basic pay as HRA.
- 8.33% of monthly basic pay as PF.
- Calculate the Gross Salary as basic pay + DA + HRA and
- Net Salary as Gross salary - PF
Print the results of all above data with headings
Soln:
public class employee
{
static void calc(int ecn, double basicPay)
{
double da = 65/100 * basicPay;
double hra = 21/100 * basicPay;
double pf = 8.33/100 * basicPay;
double grossSalary = basicPay + da + hra;
double netSalary = grossSalary - pf;
System.out.println("Employee Code Number: " + ecn);
System.out.println("Basic Pay: " + basicPay);
System.out.println("DA: " + da);
System.out.println("HRA: " + hra);
System.out.println("PF: " + pf);
System.out.println("Gross Salary: " + grossSalary);
System.out.println("Net Salary: " + netSalary);
}
}
Q32. Create a class Result and define a function maximum(int s1, int s2, int s3, int s4), parameters s1, s2, s3 and s4 are marks in four subjects obtained by a student. Find and return the highest marks out of four subjects. Define another function display() to create an object of the class and invoke the function maximum() by passing actual values to the parameters to print the highest marks obtained.
Ans:
public class Result
{
int maximum(int s1, int s2, int s3, int s4)
{
int max = 0;
max = Math.max(s1, s2);
max = Math.max(max, s3);
max = Math.max(max, s4);
return max;
}
void display()
{
Result r1 = new Result().
int max = r1.maximum(22,34,96,56);
System.out.println(“Maximum Marks: “ + max);
}
}
Q33. Write a program to input product code number(pc), unit price of the product in decimals (price) and the quantity of the product (qty). Calculate the following:
- Total cost of the product as price * qty;
- 15% Discount on the total cost.
- Net Amount to be paid after deducting the discount.
- Print Product code, unit price, quantity, total cost, discount and new amount to be paid.
Ans:
public class employee
{
void calc(int pc, double price, int qty)
{
double totalCost=0, discount=0, netAmount=0;
totalCost = price * qty;
discount = 15/100 * totalCost;
netAmount = totalCost - discount;
System.out.println("Product Code: " + pc);
System.out.println("Price: " + price);
System.out.println("Quantity: " + qty);
System.out.println("Total Cost: " + totalCost);
System.out.println("Discount: " + discount);
System.out.println("Net Amount: " + netAmount);
}
}
Q34. Write a program to input worker code number, number of hours worked and rate per hour. Calculate and print the total wages of the worker(total wages = hours worked * rate per hour).
Ans:
public class employee
{
void calc(int wc, int noh, int rph)
{
double totalWages = noh * rph;
System.out.println(“Total Wages: “ + totalWages);
}
}
Q35. A company gives rupees 5000.00 to its worker as salary and wages on number of days extra worked. Write a program to define a function public void Wages(int days, double rate), and initialize 5000.00 to a variable salary. Calculate and print the total salary of the worker (totalSalary = (days*rate) + salary). Note that, days = number of days extra worked, rate = rate per day.
Ans:
public class employee
{
void Wages(int days, double rate)
{
double salary = 5000.00, wages = days * rate;
double totalSalary = salary + wages;
System.out.println(“Total Salary: “ + totalSalary);
}
}