Practice Worksheet 5
Practice Worksheet 5
Practice Worksheet 5
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. 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
7. Every class has a constructor. Explicitly defined by the user or implicitly created by the compiler.
a. true
b. false
class Test
{
private int marks;
int grade;
protected rollno;
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
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;
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
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();
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()
{ ……..
}
}
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();
}
//Application
class studentapp
{
public 11 12 main()
{
13 ;
s1.input();
s1.calc();
s1.display();
}
}