0% found this document useful (0 votes)
76 views6 pages

OOP Test One Answer Key

The document is an answer key for a test on Object Oriented Programming and Java, covering various topics such as software development paradigms, features of OOP and Java, and the structure of a Java program. It includes detailed explanations of concepts like classes, inheritance, encapsulation, and polymorphism, as well as practical Java programming examples. Additionally, it outlines the components of the Java Virtual Machine (JVM) and provides sample code for basic Java programs.

Uploaded by

Alemseged Mammo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views6 pages

OOP Test One Answer Key

The document is an answer key for a test on Object Oriented Programming and Java, covering various topics such as software development paradigms, features of OOP and Java, and the structure of a Java program. It includes detailed explanations of concepts like classes, inheritance, encapsulation, and polymorphism, as well as practical Java programming examples. Additionally, it outlines the components of the Java Virtual Machine (JVM) and provides sample code for basic Java programs.

Uploaded by

Alemseged Mammo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CPU Business and Information Technology College

Object oriented Programing:


Test One (content: - Chapter 1 and Java the Language)
Answer Key
General Instruction
Write the answer clearly on the attached sheet of paper (Total pts.: 20)
1. Developments in software technology continue to be dynamic. New tools and techniques
are announced in quick succession. List and Explain at least three Software development
Paradigms. (2 pts.)
Answer
The three software development paradigms are:
• Structured programming Paradigm:- Structured programming encourages
dividing an application program into a hierarchy of modules or autonomous
elements, which, in turn, may contain other such elements. Within each
element, code may be further structured using blocks of related logic designed
to improve readability and maintainability. structured programming became
very popular and was the main technique of the 1980’s. Structured
programming was a powerful tool that enabled programmers to write
moderately complex programs fairly easily. However, as the programs grew
larger, even the structured approach failed to show the desired result in terms
of bug-free, easy-to- maintain, and reusable programs.
• Procedural Programming Paradigm:- Procedure oriented programming
basically consists of writing a list of instructions for the computer to follow,
and organizing these instructions into groups known as functions.
• Object Oriented Programming Paradigm:- The major motivating factor in the
invention of object-oriented approach is to remove some of the flaws
encountered in the procedural approach. OOP treats data as a critical element
in the program development and does not allow it to flow freely around the
system. It ties data more closely to the function that operate on it, and protects
it from accidental modification from outside function. OOP allows
decomposition of a problem into a number of entities called objects and then
builds data and function around these objects.
2. List, compare and explain features of object oriented programming and procedure-oriented
programming. (2 pts.)
Answer
Some Characteristics exhibited by procedure-oriented programming are:
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
1 | Page
• Employs top-down approach in program design.
Some of the features of object oriented programming are:
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are ties together in
the datastructure.
• Data is hidden and cannot be accessed by external function.
• Objects may communicate with each other through function.
• New data and functions can be easily added whenever necessary.
• Follows bottom up approach in program design.

3. List and explain in detail the six different concepts in OOP (2 pts)
Answer
There are six different concepts in OOP:
• Object
• Class
• Inheritance
• Encapsulation
• Abstraction
• Polymorphism
1. Object
Every real-world entity is an [Link] object has Behaviour (things it does or performs) and Attributes
(things that describe it).
For eg: A Chair object can have behaviour like Movement, Height & Attributes like Color, Make &
Model, and Price.
2. Class
The collection of all related objects is called a class.
Consider class as a general category which contains all the related objects inside [Link] eg: Objects like
Wheelchair, Office Chair and Wooden Chair can be a part of the "Chair" class.
3. Inheritance
The way we inherited a few qualities from our parents similarly,
a class can also inherit the qualities from a parent class.
For eg: A Phone Class can have two Child Classes: 1) TelePhone
and 2) MobilePhone. Both can inherit the "calling" behaviour.
4. Encapsulation
It means wrapping data into a single unit & securing it.
For eg: Drug Capsule wraps different medicines into a single unit and protects them from the outside
environment.
Bank Locker wraps your valuables into a single unit(locker) and protects it via passcode.
5. Abstraction
Hiding complexity from the user and showing only the relative stuff.
For Eg: In Car, all the complexity like the engine, machinery, etc is hidden from you; only relevant parts
are shown, like the brakes, accelerator, and gearbox.
6. Polymorphism
It means many forms. With the same name, it provides different forms.

2 | Page
4. List and explain six features of java Programming (2pts)
Answer
The java features given below are simple and easy to understand. They are also known as java
buzzwords.
1. Simple: Java is Simple to write and more readable and eye-catching. It has a concise, cohesive
set of features that makes it easy to learn and use.
2. Secure: Java program cannot harm another system thus making it safe. It provides a reliable means
of creating Internet applications and a secure way to access web applications.
No explicit pointer.
Java Programs run inside virtual machine sandbox.

3. Portable: Java programs can execute in any environment for which there is a Java run-time system
(JVM). Java programs OR codes can be run on any platform means OS (Linux, Window, Mac).
Java programs can be shifted over World Wide Web (e.g., applets)
4. Object-oriented: Java programming is an object-oriented programming language. Like C++
Java provides most of the object oriented features. Java is pure OOP Language.
5. Robust: Java encourages error-free programming by being strictly typed and performing run-
time checks.
There is lack of pointers that avoids security problem, automatic garbage collection, exception
handling and type checking mechanism in java. All these points make java robust.
6. Multithreaded: Java is a multi-threaded programming language which means we can develop
multi-threaded program using Java. A multi-threaded program contains two or more parts that
can run concurrently and each part can handle different task at the same time making optimal use
of the available resources
7. Architecture-neutral: The in other Programs , program is controlled by the operating system
whereas, the execution of a Java program is controlled by JVM (Java Virtual Machine).
8. Interpreted: The interpreted feature in C++ is very slow because it is interpreted line by line,
and makes the system a slower way of running a program than one that has been compiled.
9. High performance:. Byte codes are highly optimized. JVM can execute them much faster.
10. Distributed: Java was designed with the distributed environment. Java can be transmitted, run
over the internet.
11. Dynamic: Java programs carry with them large amounts of run-time type information that is used
to confirm and resolve accesses to objects at run time.

3 | Page
5. List at least six elements that are included in the structure of a Java program. (2pts)
The Elements included in structure of Java Program are:-

1. The documentation section is an important section but optional for a Java program. It
includes basic information about a Java program. The information includes the author's
name, date of creation, version, program name, company name, and description of the
program.
2. The package declaration is optional. It is placed just after the documentation section. In this
section, we declare the package name in which the class is placed. Note that there can
be only one package statement in a Java program.
3. The package contains the many predefined classes and interfaces. If we want to use any class
of a particular package, we need to import that class. The import statement represents the
class stored in the other package. We use the import keyword to import the class. It is
written before the class declaration and after the package statement.
4. We define the main() method. It is essential for all Java programs. Because the execution of
all Java programs starts from the main() method. In other words, it is an entry point of the
class.
5. We define the class. It is vital part of a Java program. Without the class, we cannot create
any Java program. A Java program may conation more than one class definition. We use
the class keyword to define the class. The class is a blueprint of a Java program.
6. Interface is used during the execution of the program. We can also decide and define the
scope of variables by using the modifiers. It defines the life of the variables.
6. What is JVM? List its five components and explain what it does. (2pts)
Java Virtual Machine – enables the computer to run Java program. It reads and interprets. Class
files and executes the program’s instruction into the native hardware platform. JVM is platform
independent (obtainable for many hardware and software platforms). Some of The activities of
JVM:-
• It is used to load the code.

4 | Page
• It Verifies and executes the code.
• And it provides run time environment.
Following are the components of JVM:
• Stack: - Temporary values are stored in frames of a stack.
• Heap: - The Heap consists of objects. The JVM has a heap that is the runtime data area
from which memory for all class instances and arrays are allocated. It is created at the
JVM start-up.
• Constant Pool: - Constant pool consists of all constant data. They comprise of
o Numbers
o Strings
o Symbolic names of classes, interfaces and fields
• Code segment: - It’s where all the codes reside.
• Program counter: - Tracking of current instruction execution at any moment is done by
Program Counter (PC)
7. In Java program we have a main program. Explain each component (2.5 pts)
public static void main(String[] args){ }
Write what each component mean
7.1 public: public means, public so that everyone can access this method.
7.2 static: Static means that this method can be called without instantiating the
class.
7.3 void: void means this method does not return anything or main() returns no
value.
7.4 main():It is a method or a function name which is configured to JVM
7.5 (String [] args): It means that arguments to the main method i.e. Command
line arguments
8. Write a Java Program having a class called Details and a method which reads first name, second
name and sex and print the complete information as “your name is XXX YYY and you are FFF”.
(2pts)
Use Method: - show Return Type: - void Parameters: - No parameters
import [Link];

public class Details {


private String fname;
private String sname;
private String sex;
void show (){
Scanner data=new Scanner([Link]);
[Link]("Enter First Name:> ");
fname=[Link]();
[Link]("Enter Second Name:> ");
sname=[Link]();
[Link]("Enter Sex:> ");
sex=[Link]();
[Link]("You name is "+fname+" "+sname+" and You are "+sex);
}

5 | Page
}
class RunDetails{
public static void main(String[] args) {
Details object=new Details();
[Link]();
}
}

9. Write a Java Program having a class called Compute and a method which initialize base as base =
2 and Exponent as exp = -2 and returns the result of base to the power of exponent. (2pts)

Use Method: - calculate Return Type: - double Parameter: - double base,double exp

public class Compute {


double calculate(double base,double exp){
return ([Link](base,exp));
}
}
class RunCompute{
public static void main(String[] args) {
Compute object = new Compute();
[Link]("The Answer is "+[Link](2,-2));
}
}

10. Write the output of the following program Segment.


public class Driver {
public static void main ( String [] args ) {

String [] strArr = new String [10];

for ( int i = str Arr . length - 1; i >= 0; i - -) {


str Arr [ i] = " b" + ( i - 1);
}

System . out . println (" Value : " + strArr [5]) ;

}
} Answer: b4

6 | Page

You might also like