Practice Worksheet 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PRACTICE WORKSHEET 5 CHAPTER : CLASSES AND OBJECTS

Q1. FILL IN THE BLANKS

1. The operator is used to instantiate a class by allocating memory to its object.


a. dot
b. new
c. this

2. is a special function which gets executed automatically every time an instance of the class is created.
a. object
b. compiler
c. constructor

3. is the default access specifier.


a. private
b. protected
c. package

4. A constructor without any parameters is called as


a. default
b. parameterized
c. actual

5. Datatypes based on fundamental datatypes are called as .


a. Primitive
b. Non Primitive
c. reference

6. are common to all objects and are shared by all objects


a. instance variables
b. class variables
c. global variables

7. have fixed memory.


a. fundamental datatypes
b. reference datatype
c. class

8. The following statement is an example of


Student s1;
a. Instantiation
b. Declaration
c. Initialization

9. Objects communicate with each other through


a. data members
b. methods
c. fields
10. The class from which properties and methods are derived is called .
a. derived class
b. sub class
c. base class

Q2. STATE TRUE OR FALSE

1. Constructors can have a return type.


a. true
b. false

2. Constructors can be overloaded


a. true
b. false

3. A member declared as private can be accessed only inside the class and by the subclasses in the same package.
a. true
b. false

4. Local variables are declared inside a class and are available to all the functions in the class.
a. true
b. false

5. Non static variables can be used in static functions


a. true
b. false

6. An UDT cannot have the main function


a. true
b. false

7. Every class has a constructor. Explicitly defined by the user or implicitly created by the compiler.
a. true
b. false

Q3. ANSWER THE FOLLOWING QUESTIONS

class Test
{
private int marks;
int grade;
protected rollno;

private void check()


{
………..
}
public void display()
{
…..
}
}
a. Can an object of the above class be created in another package?
1.YES
2. NO

b. Is variable ‘marks’ accessible in another class?


1.YES
2. NO

c. Is variable ‘grade’ accessible in another class of the same package?


1. YES
2. NO

d. Name the variables which can be accessed by the class which inherits the above class
1.marks,grade
2. grade,rollno
3.marks,rollno

e. What is the access specifier of the data member grade


1.friendly
2. package
3. both 1 and 2

f. Name the member which can be accessed in the class, same package and also any other package
1.marks
2. rollno
3.grade

Q4. Consider the class given below and answer the questions given below –

class Demo
{
int a;
static int b;

public static void hello()


{
a=10;
}

public void checkThis()


{
….
}

a. How many bytes will 1 instance of the class reserve in the memory?
1. 4
2. 8
3. 6
b. If 3 objects are created of the class, what is the total amount of memory that will be reserved?
1. 12
2. 16
3. 24

c. Name the function/s which need an instance to be created for calling.


1. public static void hello()
2. public void checkThis()
3. both 1 and 2

d. Write a statement which shows a call to the function hello() without any instance.
1. Demo d1=new Demo() ; d.hello();
2. Demo.hello();
3. Demo();

e. Name the function/s which can access the variable b


1. public static void hello()
2. public void checkThis()
3. both 1 and 2

Q5. ANSWER THE FOLLOWING QUESTIONS.

1. Trace errors in the following


class Number
{
int num;
public void Number()
{
num=10;
}
public static void show()
{
System.out.println(num);
}
}

2. Write the memory in bytes that the 3 instances of the below class will occupy
class ShowRoom
{
//datamembers
String branchName;
int branchId;
static ownerName;
}

3. class Myclass
{
private int a; public int b; protected int c; int d;
public void setdata()
{ ……..
}
private void setDataAgain()
{ ……..
}
}

Answer the following based on the class given above


a. Can an instance of the above class be created in another class in same package?
Name the members that will be available with the instance in the new class
b. Can an instance of the above class be created in another class in another package?
Name the members that will be available with the instance in the new class
c. Name the members that will be available to subclass in the same package
d. Name the members that will be available to subclass in the another package

Q6. FILL IN THE BLANKS TO COMPLETE THE PROGRAM

Define a class 'Student' described as below:


Data members/instance variables : name,m1,m2,m3 (marks in 3 subjects), maximum, average
Member methods :
(i) A default constructor to initialize the datamembers
(ii) A parameterized constructor to initialize the data members.
(iii) To accept the details of a student.
(iv) To compute the average and the maximum out of three marks.
(v) To display the name, age, marks in three subjects, maximum and average.
Write a main method to create an object of a class and call the above member methods.

import java.util.*;
class Student
{
String nm;
int m1,m2,m3,max;
double avg;

1 () //default constructor
{
nm= 2 ;
m1=0;
m2=0;
m3=0;
max= 3 ;
avg= 4 ;
}
5 Student(String n, int mk1 , int mk2, int mk3, int max1, double avg1) //parameterised
{
6 ;
m1=mk1;
m2=mk2;
m3=mk3;
max=max1;
7 ;
}

public 8 input()
{
Scanner ob=new Scanner (System.in);
System.out.println("enter details");
nm=ob.nextLine();
m1=ob.nextInt();
m2=ob.nextInt();
m3=ob.nextInt();
}

public void calc()


{
max= 9 ; //write a single line java statement using Math class
avg = 10 ;
}

public void display()


{
System.out.println("Maximum marks : "+ max);
System.out.println("Average : "+avg);
}
}

//Application
class studentapp
{
public 11 12 main()
{
13 ;
s1.input();
s1.calc();
s1.display();
}
}

You might also like