NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Q1
class DemoBox{
public static void main(String args[]){
int length;
int height;
int width;

length=12;
height=5;
width=3;

int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}
//////////////////////////////////////////////////////////////////////
Q2
class DemoBox{
public static void main(String args[]){
int length;
int height;
int width;

length=12;
height=5;
width=3;

int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);

int area;
area=2*length*height+2*length*width+2*height*width;
System.out.println("Area is : "+area);
}
}

/////////////////////////////////////////////////////////////////
Q3
//------------------------Box.java------------------------
class Box{
int length;
int height;
int width;
}

//----------------------DemoBox.java-------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();

b1.length=12;
b1.height=5;
b1.width=3;

int volume;
volume=b1.length*b1.height*b1.width;
System.out.println("Volume is : "+volume);

int area;
area=2*b1.length*b1.height+2*b1.length*b1.width+2*b1.height*b1.width;
System.out.println("Area is : "+area);

b1.length=120;
b1.height=50;
b1.width=30;

int volume2;
volume2=b1.length*b1.height*b1.width;
System.out.println("New volume is : "+volume2);

}
}

////////////////////////////////////////////////////////////////////
Q4
class DemoBox{
public static void main(String args[]){
int x;
x=100;
System.out.println(x);

Box b1;
b1=new Box();
System.out.println(b1);

b1.length=12;
b1.height=5;
b1.width=3;

int volume;
volume=b1.length*b1.height*b1.width;
System.out.println("Volume is : "+volume);
}
}




/////////////////////////////////////////////////////////////////////
Q5
class DemoBox{
public static void main(String args[]){
Box b1=new Box();

Box b2=new Box();

System.out.println(b1);
System.out.println(b2);

}
}


///////////////////////////////////////////////////////////////////
Q6
class DemoBox{
public static void main(String args[]){
Box b1;
b1=new Box();

b1.length=10;
b1.height=4;
b1.width=2;

System.out.println("Length of the box : "+b1.length);
System.out.println("Height of the box : "+b1.height);
System.out.println("Width of the box : "+b1.width);
}
}
/////////////////////////////////////////////////////////////////
Q7 Java Method
===========
//------------------------Box.java-------------------------
class Box{
int length;
int height;
int width;

void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}

//-----------------------DemoBox.java-----------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();

b1.length=12;
b1.height=5;
b1.width=3;

b1.printVolume();

int area;
area=2*b1.length*b1.height+2*b1.length*b1.width+2*b1.height*b1.width;
System.out.println("Area is : "+area);

b1.length=120;
b1.height=50;
b1.width=30;

b1.printVolume();

}
}

/////////////////////////////////////////////////////////////////
Q8 Java Method and Local Variable
==============================
class DemoBox{
public static void main(String args[]){
Box b1=new Box();

b1.length=12;
b1.height=5;
b1.width=3;
b1.printVolume();

System.out.println("end.");
}
}

//////////////////////////////////////////////////////////////
Q9 Example

class Box{
void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
int length; //Legal
int height; //Legal
int width; //Legal
}

//////////////////////////////////////////////////////////////////
Q10 Example

//--------------------------Box.java-------------------------
class Box{
int length;
int height;
int width;

void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}

void printArea(){
int area;
area=2*length*height+2*length*width+2*height*width;
System.out.println("Area is : "+area);
}
}
//---------------------------DemoBox.java-----------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();

b1.length=12;
b1.height=5;
b1.width=3;
b1.printVolume();
b1.printArea();

System.out.println("end.");
}
}

//////////////////////////////////////////////////////////////////
Q11 Example
class Box{
int length;
int height;
int width;

void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
System.out.println("box class"); //Illegal
int x; //Legal
void printArea(){
int area;
area=2*length*height+2*length*width+2*height*width;
System.out.println("Area is : "+area);
}
}

//////////////////////////////////////////////////////////////////
Q12 Method with Parameters

//--------------------------Box.java--------------------------
class Box{
int length;
int height;
int width;

void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}

void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}

void printArea(){
int area;
area=2*length*height+2*length*width+2*height*width;
System.out.println("Area is : "+area);
}
}

//---------------------------DemoBox.java---------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.setValues(12,5,3);
b1.printVolume();
}
}

//////////////////////////////////////////////////////////////////////
Q13 Example

//------------------------Box.java--------------------------------
class Box{
int length;
int height;
int width;

void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
void setLength(int l){
length=l;
}
void setHeight(int h){
height=h;
}
void setWidth(int w){
width=w;
}
void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}

void printArea(){
int area;
area=2*length*height+2*length*width+2*height*width;
System.out.println("Area is : "+area);
}
}

//-----------------------DemoBox.java--------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.setValues(12,5,3);
b1.printVolume();

b1.setLength(120);
b1.setHeight(50);
b1.setWidth(30);
b1.printVolume();
}
}

////////////////////////////////////////////////////////////////////
Q14 Example

class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.setValues(12,5,3);

Box b2=new Box();
b2.setValues(4,3,2);

b1.printVolume();
}
}

///////////////////////////////////////////////////////////////////
Q15 Default Values

class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.printVolume(); //Prints 0 =>Defalut values
}
}

byte => 0 (8bits)
short => 0 (16bits)
int => 0 (32bits)
long => 0 (64bits)
float => 0.0 (32bits)
double => 0.0 (64bits)

char => null character
boolean =>false


//////////////////////////////////////////////////////////////////
Q16 Decleration Values

//--------------------------Box.java-----------------------
class Box{
int length=5;
int height=5;
int width=5;

void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}

//-------------------------DemoBox.java--------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.printVolume();
}
}

////////////////////////////////////////////////////////////////
Q17 Method With a return value

//---------------------------Box.java-------------------------
class Box{
int length;
int height;
int width;

void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}
int getVolume(){
int volume;
volume=length*height*width;
return volume;
}
void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}

//---------------------------DemoBox.java------------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.setValues(12,5,3);

//int v=b1.printVolume();

int v=b1.getVolume();
v++;
System.out.println("New Volume is : "+v);
}
}

//////////////////////////////////////////////////////////////////
Q18 Example

class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.setValues(12,5,3);

System.out.println(b1.printVolume()); //Error
System.out.println(b1.getVolume());
}
}

//////////////////////////////////////////////////////////////////
Q19 Example

class Box{
int length;
int height;
int width;

int getVolume(){
int volume;
volume=length*height*width;
return volume;
System.out.println("end."); //Error
}
}


//////////////////////////////////////////////////////////////////
Q20 Example

class Box{
int length;
int height;
int width;

int getVolume(){
double volume;
volume=length*height*width;
return volume; //Illegal
}
}

/////////////////////////////////////////////////////////////////
Q21 Constructors

//---------------------------Box.java-------------------------
class Box{
int length;
int height;
int width;

Box(){
length=10;
height=10;
width=10;
System.out.println("Box is created...");
}

void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}
void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}

//---------------------------DemoBox.java----------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();
//b1.printVolume();
}
}

//////////////////////////////////////////////////////////////////////
Q22 Case I
======

//----------------------------Box.java---------------------------------
class Box{
int length;
int height;
int width;

void Box(){
length=10;
height=10;
width=10;
System.out.println("Box is created...");
}

void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}
void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}

//--------------------------DemoBox.java---------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box();
b1.printVolume(); //print "0"
}
}

////////////////////////////////////////////////////////////////////////
Q23 Case II
=======

class Box{
int length;
int height;
int width;

Box(){ //Compiler insert
//
}

void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}
void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}



//////////////////////////////////////////////////////////////
Q24 Case III (Constructor with parameters)
======================================
class Box{
int length;
int height;
int width;

Box(int l, int h, int w){
length=l;
height=h;
width=w;
}


void setValues(int l, int h, int w){
length=l;
height=h;
width=w;
}
void printVolume(){
int volume;
volume=length*height*width;
System.out.println("Volume is : "+volume);
}
}

//-----------------------------DemoBox.java---------------------------
class DemoBox{
public static void main(String args[]){
Box b1=new Box(12,5,3);
b1.printVolume();
}
}

///////////////////////////////////////////////////////////////////////////
Q25 Static Variable
===============

class A{
int a;
static int b;
}

class Demo{
public static void main(String args[]){
A a1=new A();
a1.a=10;
a1.b=20;

A a2=new A();
a2.a=100;
a2.b=200;

System.out.println(a1.a+" "+a1.b); //prints
System.out.println(a2.a+" "+a2.b); //prints
}
}

///////////////////////////////////////////////////////////////////
Q26 Static Variables
================

class A{
int a=100;
static int b=200;
}

class Demo{
public static void main(String args[]){
//System.out.println(A.a); //Illegal

System.out.println(A.b); //prints 200

A a1=null;
System.out.println(a1.b); //prints 200
}
}

//////////////////////////////////////////////////////////////////
Q27 Static Methods
==============
class A{
void m1(){
System.out.println("method m1");
}
static void m2(){
System.out.println("method m2");
}
}

class Demo{
public static void main(String args[]){
//A.m1();

A.m2(); //Legal m2 is static
}
}

/////////////////////////////////////////////////////////////////
Q28 Example

class A{
int a=10;
static int b=20;
void m1(){
System.out.println("a : "+a);
System.out.println("b : "+b);
}
static void m2(){
System.out.println("a : "+a);
System.out.println("b : "+b);
}
}
class Demo{
public static void main(String args[]){

}
}

/////////////////////////////////////////////////////////////////
Q29 Example
class MyClass{
int a;
static int b;
MyClass(int i,int j){
a=i;
b=j;
}
void printValues(){
System.out.println(a+" "+b);
}
}
class Demo{
public static void main(String args[]){
MyClass ob1=new MyClass(1,2);
MyClass ob2=new MyClass(10,20);
MyClass ob3=new MyClass(100,200);

ob1.printValues();
ob2.printValues();
ob3.printValues();
}
}

////////////////////////////////////////////////////////////////////////
Q30 Static Initilizer VS Object Initilizer
=======================================

class MyClass{
static int value;
static void m(){
//System.out.println(value);
}
static{
System.out.println("static block");
}
}
class Demo{
public static void main(String args[]){
MyClass c1=new MyClass();
}
}


////////////////////////////////////////////////////////////////////
Q31 Example

class MyClass{
static int value=100;
static{
System.out.println("static block");
System.out.println("block :" +value);
value=200;
}

static void m(){
System.out.println("Method : "+value);
}

}
class Demo{
public static void main(String args[]){
MyClass.m();
}
}




//////////////////////////////////////////////////////////////////////
Q32
class MyClass{
static{
System.out.println("static block");
System.out.println("block :" +value);
value=200;
}
static int value=100;

static void m(){
System.out.println("Method : "+value);
}

}
class Demo{
public static void main(String args[]){
MyClass.m();
}
}




//////////////////////////////////////////////////////////////////
Q33
class MyClass{
static{
System.out.println("static block 1");
}
static{
System.out.println("static block 2");
}
static{
System.out.println("static block 3");
}
}
class Demo{
public static void main(String args[]){
new MyClass();
}
}

////////////////////////////////////////////////////////////////////////
Q34 Object Initilizer
================
class MyClass{
int value;
{
System.out.println("instance block");
}
MyClass(){
System.out.println("Constructor");
}
}
class Demo{
public static void main(String args[]){
new MyClass();
}
}

//////////////////////////////////////////////////////////////////
Q36 Example
class MyClass{
int value=100;
MyClass(){
System.out.println("Constructor "+value);
value=300;
}

{
System.out.println("block "+value);
value=200;
}

}
class Demo{
public static void main(String args[]){
MyClass c1=new MyClass();
System.out.println("Main : "+c1.value);
}
}

//////////////////////////////////////////////////////////////////
Q37 Example
class MyClass{
static{
System.out.print("1 ");
}
{
System.out.print("2 ");
}
MyClass(){
System.out.print("3 ");
}
void m1(){
System.out.print("4 ");
}
static void m2(){
System.out.print("5 ");
}
}
class Demo{
public static void main(String args[]){
MyClass c1=new MyClass();
c1.m1();
}
}

////////////////////////////////////////////////////////////////////
Q38 Example
class MyClass{
static{
System.out.print("1 ");
}
{
System.out.print("2 ");
}
MyClass(){
System.out.print("3 ");
}
void m1(){
System.out.print("4 ");
}
static void m2(){
System.out.print("5 ");
}
}
class Demo{
public static void main(String args[]){
MyClass c1=new MyClass();
MyClass c2=new MyClass();
}
}




/////////////////////////////////////////////////////////////////////
Method Overloading
/////////////////////////////////////////////////////////////////////
Q39 Error
=====


class MyClass{
void myMethod(){
System.out.println("myMethod()");
}
void myMethod(){
System.out.println("myMethod2()");
}
}
class Demo{
public static void main(String args[]){
MyClass c1=new MyClass();
c1.myMethod();
}
}

///////////////////////////////////////////////////////////////////////
Q40 Method Overloading
==================
class MyClass{
void myMethod(){
System.out.println("myMethod()");
}
void myMethod(int i){
System.out.println("myMethod(int)");
}
}
class Demo{
public static void main(String args[]){
MyClass c1=new MyClass();
c1.myMethod();
c1.myMethod(100);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.