Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Saturday, 20 February 2021

Object-oriented Programming Quiz

Question 1: What will be the value of text3 when the following code is executed?

String text1 = "a" + "b";
String text2 = "c";
String text3 = text1 + text2 + text1;
Select one:
a. abca
b. abc
c. ababc
d. abcab

Solution: abcab


Question 2: An Object can be defined as an instance of a ___________.
Select one:
a. state
b. class
c. attribute
d. group

Solution: class 

Question 3: Which of the following class's object do you need in order to perform input operation in Java?
Select one:
a. Scanner
b. Date
c. JFrame
d. String

Solution:
Scanner

Question 4: Which of the following is a valid declaration and creation of an object of class Box?
Select one:
a. Box obj = new Box;
b. new Box obj;
c. Box obj = new Box();
d. obj = new Box();

Solution: Box obj = new Box();

Question 5: Identify from the below the states and the behaviours of a dog:

barking
breed
eating
color
name
wagging the tail
 

Solution:  Barking = behavior

Breed= state

Eating = behaviour

Color = state

Name = state

Wagging the tail = behavior

Question 6: Which of these operators is used to allocate memory for an object in Java?
Select one:
a. alloc
b. malloc
c. give
d. new

Solution: new 

Question 7: This code will compile and when run will print out "Hello world"

public class Main{
   public static void main(String args){
      System.out.println("Hello world");
   }
}
 

Select one:
a. True
b. False 

Solution: True

Question 8: What will be the output of the following Java program?


 

Select one:
a. I have a quiz today. I hope I can score good grade.
b. I have a quiz today.
c. I hope I can score good grade.
d. I have a quiz today.
e. I have a quiz today.I hope I can score good grade.

Solution: I have a quiz today. I hope I can score good grade.

Question 9: Study the following codes:


 In which line of the codes, the object is created? (Give the line number)
What is the type/class name of the object used in the program?

setModel is an example of _____________.

What is the name of the class?

What will be displayed by the statement in line 18?

What will be displayed by the statement in line 17?

What is the name of the object used in the program?

Solution:   In which line of the codes, the object is created? (Give the line number) = 10 

What is the type/class name of the object used in the program? = Car

setModel is an example of _____________. = method

What will be displayed by the statement in line 18? = 2017

What will be displayed by the statement in line 17? = Jazz

What is the name of the object used in the program? = car1

Question 10: Any entity that has _________ and _________ is known as an object. For example, a chair, pen, table, keyboard, bike, etc. and it can be physical or logical.

Select one:
a. method        behaviour
b. state        behaviour
c. name        age
d. state        property

Solution: state        behaviour

Question 11: Which of the following is/are incorrectly import the Scanner class into your program:
Select one or more:
a. import java.util;
b. import java.util.Scanner;
c. import java.util.*;
d. import javax.swing.Scanner;

Solution: import java.util;
import javax.swing.Scanner;

Question 12: What will be the displayed when the following code is executed?

System.out.println("Venue is  " + 18 / 2);
Select one:
Venue is 18/2
Venue is 9.0
Venue is 182
Venue is 9

Solution: Venue is 9

Question 13: Which of these keywords is used to make a class?

Select one:
a. class
b. struct
c. static
d. public

Solution: class



 

Saturday, 7 December 2019

Design and implement a reusable program that takes any standard Shape and returns a Rectangle that completely incloses the Shape.

Q: Design and implement a reusable program that takes any standard Shape and returns a Rectangle that completely incloses the Shape. I expect you to write a class that defines a method to handle a shape. Let's name the class as MyClass and the method as reusable. For simplicity, consider only Rectangle 2 cap D.Double and Line2D.Double whenever you need to show s Shapes in your answer. You don't have to concern about drawing shapes for this question. Your job is to answer the subquestions (a) through (d). API is on the last page. Identify required classe(s) and interface(s) to solve the problem and draw the corresponding class diagram to depict their relationship. Include specific Shapes in your class diagram. Implement the reusable program MyClass. Write a tester that calls the reusable method with two different specific Shape objects. public class Tester {public static void main (String [] args) {//your work goes here.}} Briefly explain how polymorphism is used in the execution of the reusable method.

Solution:
 
a) Rectangle and Circle both are Shape.
So we selected Rectangle and Circle as Classes and Shape as interface for both, becouse shape has common funtionality fot both.
Classes Rectangle is a Shape and Circle is a Shape so both has "IS A" relationship with Shape
b)Implematation =>
//================== Shape.java ===========================//
public interface Shape {
   public double getArea();
   public double perimeter();
}
//========================== Rectangle.java ===========================//
public class Rectangle implements Shape {
   private double height;
   private double length;
 
   public Rectangle() {
     
   }
   public Rectangle(double h,double l) {
       height = h;
       length = l;
   }
 
   @Override
   public double getArea() {
       return height*length;
   }
   @Override
   public double perimeter() {
       return 2.0d*(height+length);
   }
   public double getHeight() {
       return height;
   }
   public void setHeight(double height) {
       this.height = height;
   }
   public double getLength() {
       return length;
   }
   public void setLength(double length) {
       this.length = length;
   }
}
//============================= Circle.java ========================//
public class Circle implements Shape {
   private double radius;
   final private double PI = 22.0d/7.0d;
   public Circle() {
     
   }
   public Circle(double r) {
       radius = r;
   }
   @Override
   public double getArea() {
       return PI*radius*radius;
   }
 
   @Override
   public double perimeter() {
       return 2.0*PI*radius;
   }
   public double getRadius() {
       return radius;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
   public double getPI() {
       return PI;
   }
}
c) Test Program
//============================= ReusableTest.java==================================//
public class ReusableTest {
   public static void main(String[] args) {
       Shape[] shapes = new Shape[2];
       shapes[0] = new Rectangle(40.0d, 30.0d);
       shapes[1] = new Circle(20.0d);
       for(int i = 0;i<2;i++){
           System.out.println("Shape "+i+" Area :"+shapes[i].getArea());
           System.out.println("Shape "+i+" Perimeter :"+shapes[i].perimeter());
       }
   }
}
d)
We have careated Shape refrence variable and storing the object of circle and rectangel
by using shape only we are calling method area and perimeter but actual method is called from respective objcet only, so that is the example of run time polimorphysm