Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

MDI DICT PROGRAM

OBJECT ORIENTED PROGRAMMING WITH JAVA

INSTRUCTOR: BUBA BOJANG


1.
a. Explain the relationship between a class and an object.
ANS: A class defines the property and behaviour for the object an object exhibits the
property and behaviour defined by its class
b. Write an example of a class. In the class, define the main method and create an object of
the class in the main method.
ANS: Class Bicycle {public static void main(string[]args){
Bicycle bike1=new Bicycle ();
c. Differentiate between instance variables and class variables give an example to support
your point.
ANS: Instance belongs to a class and class belongs to a class.
d. Use a loop of your choice to print the odd numbers between 1 and 100.
ANS: for(int i=1,1<=100; i+=2) { }
e. What will be result of A in each case:
i. double A = 2/3; =0 ii. double A = (double)2/3;=0.67 iii. int A = -11.7; = ERROR iv. int A =
(int)-11.7;= -11

2. class SquareChange {
public static void printSquare(int x){
System.out.println("printSquare x = " + x);
x = x * x;
System.out.println("printSquare x = " + x);
}
public static void main(String[] arguments){
int x = 5;
System.out.println("main x = " + x);
printSquare(x);
System.out.println("main x = " + x);
}
} What's the result of x in these three statements?
ANS: 5,25,25,5

3. for(int x = 0; x <= 13; x += 2){


if(x == (13 / 2)){
continue;
}if(x == 13){
break;
}
System.out.println("#: " + x );
}
What will be the result of executing this code?
ANS: #:0 #:2 #:4 #:6 #:8 FALSE

You might also like