OBJECT-ORIENTED PROGRAMMING Flashcards

1
Q

Everything in OOP is associated with classes and objects, Describe the differences between Class and Object.

A

CLASS
A class is a template for
objects.

OBJECT
An object is a member or an
“instance” of a class.

CLASS
A class defines object properties including a valid range of values, and a default value.

OBJECT
An object has a state in which all of its properties have values you either explicitly define or that are defined by default settings.

CLASS
A class also describes object
behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

State TWO (2) object-oriented programming characteristic and explain the characteristics.

A

ABSTRACTION
* Process of taking away or removing characteristics from an object in order
to reduce it to a set of essential characteristics.

ENCAPSULATION (DATA HIDING)
* Binding or wrapping‐up of data and associated methods into a single unit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The goal of the SDLC is to produce superior software that meets and all customer expectations and demands. List FIVE (5) stages of Software Cycle.

A
  • Analysis
  • Design
  • Coding
  • Testing
  • Operation and Maintenance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Reserved words in Java serve as a code key. These words can’t be use anything else because they’re predefined. State FIVE (5) java reserved words.

A
  • class
  • boolean
  • int
  • double
  • float

* public
* static
* void
* if
* else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

```
public class Cinema
{
private String movieHall;
private String movieTitIe;
private double price;
private String modePayment; // QRPayment, Cash private String membership; //
definition of methods
}

~~~

A parameterized/normal constructor to initialize the object values.

A
public Cinema (String movieHall, String movieTitle, double price,
String modePayment, Stringmembership)
{
movieHall = mH;
movieTitle = mT;
price = p;
modePayment = mP;
membership = m;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

```
public class Cinema
{
private String movieHall;
private String movieTitIe;
private double price;
private String modePayment; // QRPayment, Cash private String membership; //
definition of methods
}

~~~

A mutator and accessor methods for all data members.

A

Accessor (Getter)
~~~
public String getMovieHall()
{
return movieHall;
}
public String getMovieTitle()
{
return movieTitle;
}
public double getPrice()
{
return price;
}
public String getModePayment()
{
return modePayment;
}
public String getMembership()
{
return membership;
}
~~~

Mutator (Setters)
~~~
public void setMovieHall(String movieHall)
{
movieHall = mH;
}
public void setMovieTitle(String movieTitle)
{
movieTitle = mT;
}
public void setPrice(double price)
{
price = p;
}
public void setModePayment(String modePayment)
{
modePayment = mP;
}
public void setMembership(String membership)
{
membership = m;
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The method calculateDiscount() to return the discount rate based
on the mode of payment and membership of every customer.

The table at Google Classroom ‘Exercise’ file.

A
public calculateDiscount()
{
double price;
double discount;
if (modePayment.equalsIgnoreCase("QR"))
{
if (membership.equalsIgnoreCase("True"))
{
discountRate = price*7/100;
}
else
{
discountRate = price*3/100;
}
}
else
if (modePayment.equalsIgnoreCase("Cash"))
{
if
(membership.equalsIgnoreCase("True
")) {
discountRate = price*10/100;
}
else
{
discountRate = price*5/100;
}
}
return discountRate;
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a program to ask user to input and store all information needed.

The table at Google Classroom ‘Exercise’ file.

A
main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Are you member? ");
String membership = input.next();
System.out.println(“Please insert price");
double price = input.nextDouble();
String movieHall = scanner.nextLine();
System.out.println("QR or Cash");
String modePayment = input.next();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A printer method to display all the information.

The table at Google Classroom ‘Exercise’ file.

A
public static void Print()
{
System.out.println("Movie Hall: " + movieHall);
System.out.println("Movie Title: " + movieTitle);
System.out.println("Price: " + price);
System.out.println("Mode of Payment: " + modePayment);
System.out.println("Membership: " + membership);
System.out.println("Discount Rate: " + (calculateDiscount() *
100) + "%"); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create an array of object hallCapacity that can store hundreds (150) data.

A

Hall [ ] hallCapacity = new Hall[150];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Java package for input-output program.

A

import java.io.*;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

To open a file name** stock.txt**t for reading, totalstock.txt for writing, and printing process. (Include the class declaration).

A
import java.io.*;
import java.util.*;
public class scoreboard
{
public static void main(String[] args) throws Exception
{
try{
BufferedReader fr = new BufferedReader (new FileReader("score.txt"));
Bufferedwriter br = new Bufferedwriter (new FileWriter ("scoreout.txt"));
Printwriter pr = new PrintWriter (br) ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

To close all file streams in used.

A
fr.close();
br.close();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

To handle exception for file input-output processes.

A
catch (FileNotFoundException fnf) {
    System.out.println(fnf.getMessage());
} 
catch (EOFException eof) {
    System.out.println(eof.getMessage());
} 
catch (IOException io) {
    System.out.println(io.getMessage());
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a program that will calculate the length of the array for element show below:
~~~
int [ ][ ] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7}
}
~~~

A
public class ArrayLengthCalculator {
 public static void main(String[] args) {
 int [ ][ ] a = {
 {1, 2, 3},
 {4, 5, 6, 9},
 {7}
 };
 int totalLength = 0;
 for (int i = 0; i < a.length; i++) {
 totalLength += a[i].length;
 }
 System.out.println("Total length of the array: " + totalLength);
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Give FIVE (5) differences between Structured Programming and OOP.

A

1
Structure Programming
Focuses on process
OOP
Focuses on data

2
Structure Programming
Using top-down approach
OOP
Using bottom up approach

3
Structure Programming
Programs are divided into small self contained functions
OOP
Programs are divided into small
entities called objects

4
Structure Programming
Less secure as there is no way of data hiding
OOP
More secure since it has data hiding features

5
Structure Programming
Can solve moderately complex
programs
OOP
Can solve any complex programs

17
Q

State one object and give two (2) examples for
each of the elements.

A

**Object: **
Car

Attribute:
-White
-Tesla

Behavior:
- Accelerate: The car can accelerate when the gas pedal is pressed.
- Brake: The car can apply brakes to slow down or stop.
State:
- 260 km/h
- Range <600 km

18
Q

State FOUR (4) object-oriented programming advantages.

A
  1. Save time and high productivity
  2. Secure program codes
  3. Easy to partition the work based on objects
  4. Easy to upgrade from smaller to larger system
19
Q

Write the coding for all question below.
Write a program to input marks of five subjects Physics, Chemistry,
Biology, Mathematics and Computer. Calculate percentage and grade
according to following:
Percentage <= 90% : Grade A
Percentage <= 80% : Grade B
Percentage <= 70% : Grade C
Percentage <= 60% : Grade D
Percentage <= 40% : Grade E
Percentage < 40% : Grade F

A

public class Main (
public static void main(String[] args) { Scanner in = new Scanner(System.in);

System.out.println(“Enter the marks of five subjects: :\n”);

// To store the values of five subjects
float sub_1 in.nextFloat();
float sub 2 in.nextFloat();
float sub 3 in.nextFloat();
float sub 4 in.nextFloat();
float sub 5 in.nextFloat();

float total;
float average;
float percentage;
char grade;

// Calculate total, average and percentage total = sub_1+ sub_2+ sub_3+ sub_4+ sub_5;
average (float) (total / 5.0);
percentage= (float) ((total / 500.0) * 100);

// It will calculate the Grade
if (percentage <= 90) {
grade = ‘A’;
} else if (percentage <= 80) {
grade = ‘B’;
} else if (percentage <= 70) {
grade = ‘C’;
} else if (percentage <= 60) {
grade = ‘D’;
} else if (percentage <= 40) {
grade = ‘E’;
} else {
grade = ‘F’;
}

System.out.println(“Total marks: “ + total);
System.out.println(“Average: “ + average);
System.out.println(“Percentage: “ + percentage + “%”);
System.out.println(“Grade: “ + grade);
}
}

20
Q

Identify the error in the following code and explain the error of the
code.
~~~
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
name = name;
age = age;
}
~~~

A
this.name = name;
this.age = age;

Use this.name = name; and this.age = age; to correctly assign the constructor parameters to the class’s private fields.

21
Q

Write a program to calculate the sum of 3 numbers.

A
public class Main(
public static void main (String[] args) {
int num1=7;
int num2=9;
int num3=123;
int sum=num1+num2+num3;
System.out.print("the sum is ");
System.out.println(sum);
}
}
22
Q

Write a program to calculate BMI. The user will need to input their name, height (in meter), weight (in kg) and age. The program will then print/display all the user information and user BMI.

The BMI formula is as follow:
BMI = weight / (height * height)

A
import java.util.Scanner;
public class Example (
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
System.out.print ("Input weight in kilogram: "); 
double weight = sc.nextDouble();
System.out.print ("Input height in meters: ");
double height = sc.nextDouble();
double BMI = weight / (height height);
System.out.print("The Body Mass Index (BMI) is + BMI + kg/m2");
}
}