![](https://simplycoding.in/wp-content/uploads/2021/06/Questions-on-Class-as-the-Basis-of-all-Computation-1-1.jpg)
Questions on Class as the Basis of all Computation
- Categories OOPS concepts, Java, OOPS
- Identify the statements listed below as assignment, increment, method invocation or object creation statements.
- petrol++;
- System.out.println ( “Simply”);
- cost = 720.50;
- Car myCar=new Car();
Ans.
- increment statement.
- method invocation statement.
- Assignment statement or variable initialization.
- new Object instantiation
- Read the following class carefully and answer the questions that follows:
class school{
int roll,mark;
String name;
school(){ //function 1
roll= 3856;
}
static void data(){ //function 2
mark=0;
name= "student"; //statement1
}
void show(){ //function 3
System.out.println(roll+" "+name+" "+mark);
}
}
- Name the instance variables.
- What is the name given to function 3?
- Name the class method? Explain why?
- Explain statement 1
Ans.
- The instance variables are roll, mark and name
- The function 3 is an instance/member function or user-defined function
- The name of the class method is data(). Because the keyword static makes a method as static or class method.
- The statement 1 is assignment or initialization of string constant to instance variable of string type name.
- Read the following class carefully and answer the questions that follows:
class school{
int roll;
String name;
school(){ //function 1
roll= 3856;
}
static void data(){ //function 2
double mark=0.0;
name= "student"; //statement1
}
void show(){ //function 3
System.out.println(roll+" "+name);
}
}
- Name the local variables.
- What is the name given to function 1?
- Name the global variables.
- Can variable mark be used in method show()? Justify.
Ans.
- The local variable is mark.
- The function 1 is a default or non-parameterized constructor
- The global variables are roll and name.
- No, because mark is a local variable to function data() so it cannot be used inside function show().
- Identify the type of constructors that are implemented in following class
Class Simply {
Int x, y;
Simply ();
Simply (Simply& b);
}
Ans.
Simply () – Default Constructor
Simply (Simply& b) – Copy constructor
- Give the output of the following program code when function Display() is invoked? Justify.
public class simply {
static double R=78.3;
public static void Show(){
double R= 75.0;
System.out.println(R);
}
public static void Print(){
System.out.println(R);
}
public void Display(){
Show();
Print();
}
}
Ans.
75.0 (variable R is assigned 75.0 and act as local variable)
78.3 (it has only print statement , so in this case the Global value R=78.3 activate and print)
- Consider the following class:
public class myClass{
public static int x=3, y=4;
public int a=2, b=3;
}
- Name the variables for which each object of the class will have its own distinct copy.
- Name the variables that are common to all objects of the class.
Ans.
- The variables: ‘a’ and ‘b‟ //(because these are non-static or instance variables)
- The variables : ‘x’ and ‘y‟ //(because these are static or class variables.)
- Rewrite the following program after removing the errors, underlining each correction:
class My Class
{
int a, b;
void initialize( )
{
MyClass
a=5;
b=6;
}
void show ( )
{
System.out.println (a+ ‘’ ‘’ + b);
}
static void main( )
{
My Class ob = new My Class ( );
initialize ( );
show ( ). ob;
}
}
Ans.
class MyClass
{
int a, b;
void initialize( )
{
a=5;
b=6;
}
void show ( )
{
System.out.println (a+ ‘’ ‘’ + b);
}
static void main( )
{
MyClass ob = new MyClass( );
initialize( );
ob.show ( );
}
}
- Which among the following are invalid class names in Java? State with reasons.
- Simply Coding
- 1My Simply
- MySimply$
- Mysimply #
- My@ simply
Ans.
- Invalid, as a variable name cannot have a blank space.
- Invalid, as a variable name cannot begin with a digit.
- valid
- Invalid, as a variable name cannot have a special character, like #.
- Invalid, as a variable name cannot have a special character, like @.
- Consider the following code and answer the questions that follow:
class academic { int x,y;
void access()
{
int a,b;
academic student=new academic();
System.out.println(“Object Created”);
}
}
- What is the object name of the class?
- Name the instance variables used in the class.
- Write the name of local variables used in the program.
- Give the type of function being used and its name.
Ans.
- student
- x and y
- a and b
- Procedural function or pure function.
Name: access()
- Consider the following code and answer the questions that follow:
class vxl
{
int x,y;
void init( )
{ x=5; y=10;
}
protected void access( )
{
int a=50, b=100;
vxl vin=new vxl( );
vin.int( );
System.out.println(“Object created”);
System.out.println(“I am X=”+vin.x);
System.out.println(“I am Y=”+vin.y);
}
}
- What is the object name of the class vxl?
- Name the local variables of class.
- What is the access specifier of method access( )?
- Write the output of the above program.
Ans.
- vin
- a and b
- protected
- Output:
Object created
I am X=5
I am Y=10
- Find the errors in the program given below and rewrite the corrected form:
My class
{
int a;
int b;
void display()
{
System.out.printIn(a+“ ”+b);
}
static void display2()
{
System.out.println(a+“ ”+b);
}
public static void main(String args[ ])
{
My class ob1=new My class( );
display1().ob1;
display2().ob2;
}
}
Ans.
class Myclass { int a; int b; void display1( ) { System.out.println(a+“ ”+b); } void display2( ) { System.out.println(a+“ ”+b); } public static void main(String args[ ]) { Myclass ob1=new Myclass( ); Myclass ob2=new Myclass( ); ob1.display1(); ob2.display2(); } }
- In the program given below:
class MyClass { static int x = 7; int y = 2; public static void main(String args[ ]) { MyClass obj = new MyClass(); System.out.println(x); obj.sampleMethod(5); int a= 6; System.out.println(a); } void sampleMethod(int n) { System.out.println(n); System.out.println(y); } }
State the name and value of the:
- (i) method argument or argument variable.
- (ii) class variable.
- (iii) local variable.
- (iv) instance variable.
Ans.
- (i) main() =String args[] value=none
sampleMethod=int n value=5 - (ii) x value=7
- (iii) a value=6
- (iv) y value=2
- (i) main() =String args[] value=none
You may also like
![Questions on Encapsulation Questions on Encapsulation](https://simplycoding.in/wp-content/uploads/2021/06/Questions-on-Encapsulation-300x200.jpg)
Questions on Encapsulation
4 July, 2021
![Constructor Programs Constructor Programs](https://simplycoding.in/wp-content/uploads/2021/06/Constructor-Programs-300x200.jpg)
Constructor Programs
3 July, 2021
![Java Object Oriented Concepts Java Object Oriented Concepts](https://simplycoding.in/wp-content/uploads/2020/06/Java-Object-Oriented-Concepts-300x200.jpg)
Java Object Oriented Concepts made easy
22 June, 2021