0% found this document useful (0 votes)
25 views14 pages

AOOP

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)
25 views14 pages

AOOP

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
You are on page 1/ 14

AOOP

Quiz 1 ● Identify the validity of the given statements.


● It is the concept of hiding details you do not I. All instances has the same attribute
care about as a user. Abstraction values.
● Identify the validity of the given statements. II. All instances has the same methods. Only
I. Runner program is not an object. statement II is True
II. Method parameters must have different
names from the class fields. Neither
statement is True PRELIM EXAM
● It is the abstraction of a real world concept
into programming. Object ● What do you call an entity that has two
● These are the capabilities of an object. characteristics (states and behavior)?
Behavior Objects
● It is the blueprint of an object. Class ● What is the term referring to blueprint from
● Identify the validity of the given statements. which individual objects are created? Class
I. Constructors has the same name as the ● What is the process by which we can reuse
class. the functionalities of existing classes to new
II. Void return type does not provide any classes? Polymorphism
value. Both statements are True ● What is the other term for base class? parent
● What is the value of an object types in the class
default constructor? null ● What is the method in which is performed in
● Identify the validity of the given statements. the same class where we have multiple
I. A default constructor is provided when methods with the same name but different
constructors are defined in a class. parameters? method overloading
II. To encapsulate an object, fields must be ● What do you call a method in which it uses
publicly available. Neither statement is True the inheritance and can have multiple
● These are methods that retrieves field methods with the same name in parent and
values. accessor child classes? method overriding
● This property states that objects are smart ● What is the term which refers to the
on their own but requires others to tell what it technique of hiding internal details and
needs to do. Anthropomorphic showing functionalities? abstraction
● It is the keyword that points to the current ● What is that object-oriented approach, in
instance. this which binds the data members (attributes)
● Identify the validity of the given statements. and methods together and restricts direct
I. Constructors can be overloaded. access to important data? encapsulation
II. Support methods are public. Only ● Which of the following statements are valid?
statement I is True I. Objects can command themselves.
● Where does overloaded methods differ? II. Override annotation is optional when
parameter list overriding a method. only statement II is true
● It is the representation of object ● Which of the following is not true about
characteristics on Java programming. object- oriented programming in Java? It is
Attributes classified as a compiler language
● Which of the following statements are true
about objects?
AOOP

I. Objects can command themselves. ● Which of the following method allows us to


II. Override annotation is optional when directly access public methods from the
overriding a method. Only statement II is true superclass instance? super( )
● What is the term that describes the current ● Which of the following modifier is capable of
characteristics of the specified object? making the designated field or method
attribute or state visible of the current class to its subclasses?
● What do you call the possible actions that protected
your object is able to do? behavior ● Which of the following allows us to
● What modifier makes the object publicly collectively call all the instances of the
available? public subclasses as the superclass type which
● What do you call an optional special method means “many forms”? Polymorphism
is called when an instance is being created ● What Java code provides only method
in a process? instantiation signatures in the body and uses the
● What keyword specifically points to the implementer to define the actions for the
current instance from which the method was corresponding methods? interfaces
triggered? this ● Which of the following statements are valid?
● What do you call the process of restricting I. Methods signatures are defined instead of
the access to data within the object which whole method.
protects the object data from unwanted II. Interfaces uses implements keyword.
modifications? encapsulation III. A class can inherit from multiple
● Which method is used for data retrieval? interfaces. I, II and III are all valid
accessor/getter ● What do you call an idea or concept that can
● What do you call a relation between 2 be realized and is able to represent a set of
objects where in one object is being used by entities? model
another object and/or vice versa? ● Which of the following refers to the
association realization of the given model? instance
● What relationship in which 1 object relies on ● What do you call a block of codes similar to
the existence of the other object? a method and when it is called, creates an
composition instance of the class? constructor
● Which of the following shows a hierarchical ● Which of the following is a blueprint of an
order between the objects and an either be object and are used to translate objects into
the parent class or the child class? code? class
inheritance ● What language was created by James
● What is the term which refers to the base Gosling et al in 1991at Sun Microsystems?
class to be used and also known as Java
"superclass"? parent ● Which of the following is typically a
● What class the derived from the parent or general-purpose program/s that run on any
subclass? child/subclass machine where the Java runtime
● What is keyword used to apply inheritance environment (JRE) is installed? Java
relationship in which classes must b technology applications
defined? extends ● Which of the following provides the hardware
platform specifications to which you compile
AOOP

all Java technology code? Java Virtual ● What is the output of the given program?
Machine (JVM) Object Oriented Programming hello Enjoy
● What do you call a special language that can Programming in Java
be understood by the Java Virtual Machine
(JVM)? Bytecode
Java
● Which of the following is responsible for
freeing any memory? Garbage Collection public class JavaSample{
thread
● Which of the following is responsible for public static void
loading all classes needed in the Java main(String args[]){
program? Class loader
● What is the output of the given program? String str = "Object
CS002-OOP Oriented Programming";

char
Java arrCh[]={'h','e','l','l','o
'};
public class Hello {
String str2 = new
String name;
String(arrCh);
//Constructor
String str3 = new
Hello(){ String("Enjoy Programming
in Java");
this.name =
"CS002-OOP";
System.out.println(str);
}

public static void System.out.println(str2);


main(String[] args) {

Hello obj = new System.out.println(str3);


Hello();
}

System.out.println(ob }
j.name);

} ● Which of the following is not a keyword in


java? Boolean
AOOP

● What is the size of double variable? 64 bit ● This is the keyword used for making fields to
● What is the default value of char variable? be available to subclasses. protected
'\u0000' ● Write a Java program to create a class
● Which of the following is true about protected called Animal with a method named move().
access modifier? Variables, methods and Create a subclass called Cheetah that
constructors which are declared protected in overrides the move() method to run.
the superclass can be accessed only by its
child class.
● What is function overriding? Both A and B Java
(If a subclass uses a method that is already class Animal {
provided by its parent class, it is known as
public void move() {
Method Overriding, If a subclass provides a
specific implementation of a method that is
already provided by its parent class, which is System.out.println("This
known as method overriding.) animal moves.");
● Which of the following is a primitive data type }
in Java? Int }
● Which of the following is the smallest
primitive data type in Java? byte
class Cheetah extends
● What character data type cannot store the
following value? 75 Animal {
● What is the output of the following code @Override
snippet? 17Hello public void move() {

System.out.println("The
Java cheetah runs fast!");
}
int x = 7;
}
int y = 10;
public class Main {
System.out.println(x + y +
public static void
"Hello");
main(String[] args) {
new
Animal().move();
new
Quiz 3 Cheetah().move();
((Animal) new
● What type of relationship represents objects Cheetah()).move();
to be associated with one another and able
}
to exist individually? Aggregation
AOOP

● It is the conventions used for Generic value


data. V
} ● What interface is used to mark the class as
available for conversion into byte stream
format? Serializable
● It is the class where fields and methods will ● It is the keyword that limits the Generic to the
be inherited from. Superclass subclass of the specified type. extends
● This is the root class of all objects. Object ● It is the pair of symbols that enclose a type in
● Which is an example of composition? House generics. <>
and Window ● Which of the following is not a limitation of
● It is the concept of assigning a subclass to generics. Allow type inheritance.
its superclass type. Polymorphism ● Which of the following is not included as an
● This keyword is used when direct access to advantage of Java Generics? Drag-and-drop
the parent class is needed. super ● Which of the following is not a purpose of
using generics? Specification over Type
● What will be the output of this program? 10

Quiz 6

● If the model is defined as Observable and


the view is defined as Observer, we still need
to indicate to which model the view should
listen to. This is done by calling
addObserver method on the model.

Java
public class Controller extends
JFrame{
● It represents the "Is-a" relationship. private Ball model;
Inheritance private View view;
private JButton btnStep;
public Controller(){
Quiz 5 model = new Ball(1, 1, 1,
1, 10);
● It is the process of reconstructing the object view = new View(model);

from the serialized state. Deserialization btnStep = new


JButton("Step");
● Which of the following is a way to sort in
Java? All of the given choices ( using loops,
add(view);
using sort ( ) method of Array class, using
add(btnStep,
sort method of Collections class)
BorderLayout.SOUTH);
● It is the Java class capable of reading data in
byte stream format. ObjectInputStream
AOOP

a class.
II. Observer is an interface and Observable
is a class. Only statement II is True
model.addObserver(view);
● The part of the application that represents
the presentation of data. View
btnStep.addActionListener(new
● a process in which the current state of the
ActionListener() { Object will be saved in the stream of bytes.
public void Serialization
actionPerformed(ActionEvent ae){ ● The class enables a mechanism that allows
the model to broadcast or notify the target
model.move(view.getWidth(), when changes to itself occur. Observable
view.getHeight()); class
} ● Instead of allowing a specific type, a generic
}); object accept different type of parameter.
} True
} ● A component that does work is needed to
solve the problem- defines the Business
Logic of the application. Model

● The given code below copies the data from a


source to the destination. This cannot be MIDTERM EXAM
done with an upper bound limit since we ● Instead of allowing a specific type, a generic
want them both to be generic. True object accept different type of parameter.
True
Java ● In this syntax, the capital T is a variable
public<T, S super T> S[] copy(T[] known as type parameter which is capable of
src, S[] dest){ accepting any type. public class Sample<T>{
for(int i=0; i<src.length; /* class code */ }
i++){ ● What does the code below mean? the
dest[i] = src[i]; destination collection will only accept the
} superclass types of T.
return dest;
}

Java
public<T, S super T> S[] copy(T[]
● These are nameless classes defined inside src, S[] dest){
for(int i=0; i<src.length;
another class. Anonymous Inner Class
i++){
● Which is not an advantage of applying MVC?
dest[i] = src[i];
Serial Development
}
● Which of the following statements are true?
I. Observable is an interface and Observer is
AOOP

● These are classes defined inside other


classes. Inner Class
return dest;
● Classes are defined at the method level.
}
Method Local Inner Class
● Nameless and use once classes commonly
found as method arguments. Anonymous
● The given code below copies the data from a Inner Class
source to the destination. This cannot be ● It is the component that focuses on the logic
done with an upper bound limit since we of the application. Model
want them both to be generic. True ● Which is not an advantage of applying MVC?
Serial Development
● It is the class that is inherited by models to
Java enable sending notification to update views.
public<T, S super T> S[] copy(T[] Observable
src, S[] dest){ ● What method is implemented by view to
for(int i=0; i<src.length; apply changes from models? void
i++){ update(Observable obj, Object data)
dest[i] = src[i]; ● Which of the following is the main concern of
}
the controller? Handling user interaction
return dest;
● These are nameless classes defined inside
}
another class. Anonymous Inner Class
● Which of the following is not a reason to
have an inner class? To lessen code files
● a process in which the current state of the
produced.
Object will be saved in the stream of bytes.
● Which of the following statements are true?
Serialization
I. Observable is an interface and Observer is
● An application architectural pattern. MVC
a class.
● A component that does work is needed to
II. Observer is an interface and Observable
solve the problem- defines the Business
is a class. Only statement II is True
Logic of the application. Model
● It is the architecture or pattern that divides
● The class enables a mechanism that allows
an application to 3 major components: view,
the model to broadcast or notify the target
controller and model. MVC Architecture
when changes to itself occur. Observable
● It is the component which can provide a
class
graphical representation of the data. View
● To be able to listen to model changes, Java
● What is a method of allowing abstraction
provided an interface for defining action
over types? Generics
when a model update has been received it is
● What are the purposes of using generics?
called Observer
Choose 3. (Strengthens Relationships,
● The part of the application that represents
Removes the need of casting, Declaration of
the presentation of data. View
Programmers Intent)
● The component that handles user
● This syntax to apply generics? True
interaction. Controller
public class Sample<T>{ /* class code */ }
AOOP

● Which generic bound uses extends keyword ● Always mix Model code with GUI code.
and it is the limit that states that the False
accepted type must be a subtype of the ● A Model does the "business logic". True
given class? upperbound
● Which of the following are the uses for
different purpose of byte stream? Send to
Database and DBMS
other platform over network and Write to
A database is a shared, integrated computer structure
disk
that houses:
● Which generic bound uses super keyword
and it is the limit that states that the ● Raw Data
accepted type must be supertype of the ● Metadata
given class? lowerbound
● What is the pair of symbols that enclose a A DBMS or Database Management System is a
type in generics? <> collection of programs that manages the database
● Which of the following is not a limitation of structure and controls access to the data. It allows
generics? Allow type inheritance users to perform the following tasks:
● What is the keyword that limits the generic to
● Data Definition: Helps in the creation,
the subclass of the specified type? extends
modification, and removal of definitions that
● Which of the following is not a required
organize the data in the database.
modifier for serialVersionUUID field? private
● Data Updation: Enables insertion,
● What is the keyword that relates a field to the modification, and deletion of actual data in
class not to the instance? static the database.
● What is the java class that is capable of ● Data Retrieval: Facilitates retrieving data
reading data in byte stream format? from the database for use in applications.
ObjectInputStream ● User Administration: Assists in registering
● What interface is used to mark the class as and monitoring users, enforcing data
available for conversion into byte stream security, monitoring performance,
format? Serializable maintaining data integrity, handling
● Which of the following is not a purpose of concurrency control, and recovering data
using generics? Specification over Type corrupted by unexpected failures.
● What describe the higher -level organization
of solutions to common problems? Design There are many DBMS options to choose from:
Patterns
● MySQL
● What provides for user input and
● MSSQL
modification? Controller
● Oracle DBMS
● The model should not be independent of ● PostgreSQL
both the Controller and the View. False ● MariaDB
● The controller decides what the model is to ● SQLite
do. True ● IBM DB2
● The View should not display what the
Controller thinks is happening. True
AOOP

The choice of DBMS depends on the application. For Applications


our course, we will use MySQL. Java can create different types of executables, such
as:

● Java Applications
Tables ● Java Applets
Most data in a database is stored in tables. ● Java Servlets
● Java Server Pages (JSPs)
A table is a two-dimensional structure composed of ● Enterprise JavaBeans (EJBs)
rows and columns:

● Rows represent entity occurrences or object


instances. Architecture
● Columns represent attributes, each with a The architecture of a Java database application
distinct name. Values must match the involves the following steps:
specified data type for each column.
1. The Java application connects to the JDBC
Each row should ideally have a Primary Key, which API.
serves as a unique identifier for each entity in the 2. JDBC API finds and matches the appropriate
table. database driver through the JDBC Driver
Manager.
3. The driver connects and communicates with
the database.
JDBC
JDBC stands for Java Database Connectivity. To communicate with the database, it is necessary to
understand SQL.
It is a standard Java API for database-independent
connectivity between the Java programming language
and a wide range of databases.
SQL
SQL stands for Structured Query Language.
Usage It is a standardized language used to perform
The JDBC library includes APIs for common database operations on a database, such as:
tasks:
● Creating entries.
● Making a connection to a database. ● Reading content.
● Creating SQL or MySQL statements. ● Updating content.
● Executing SQL or MySQL queries in the ● Deleting entries.
database.
● Viewing and modifying the resulting records. These operations are often referred to as CRUD.

SQL is case-insensitive, though SQL statements


might vary slightly across different databases.
AOOP

Operations ● CREATE TABLE: Defines a table in the


database.
Using SQL, we can perform the following operations:
Syntax:
● Data Definition Language (DDL): Deals
with defining database objects. sql
● Data Manipulation Language (DML):
Manages data within the database. Copy code

DDL CREATE TABLE table_name (

DDL defines the structure of data in the database: field_name data_type


constraints,
● CREATE: Creates database objects.
● DROP: Deletes database objects. ...

);
CREATE Syntax and Examples
Example:
● CREATE DATABASE: Creates a new
sql
database.
Copy code
Syntax:

sql CREATE TABLE students (

Copy code id INT PRIMARY KEY


AUTO_INCREMENT,
CREATE DATABASE database_name;
name VARCHAR(255) NOT NULL,

year_level INT NOT NULL,


Example:
program VARCHAR(255) NOT NULL
sql
);
Copy code
Data Types:
CREATE DATABASE school;
● INT: Stores whole numbers.
● VARCHAR: Stores variable-length strings
This command creates a database named school. (max length defined).
AOOP

● DOUBLE: Stores numbers with decimal This command deletes the school database,
parts. including all its objects.

Constraints: ● DROP TABLE: Deletes a table instance.

● PRIMARY KEY: Makes a field unique and Syntax:


not null.
● UNIQUE: Requires unique values for the sql
field.
● NOT NULL: Ensures the field always Copy code
contains a value.
DROP TABLE table_name;
● AUTO_INCREMENT: Automatically assigns
incremental values to the field.
Example:
This statement creates a table named students with
sql
fields: id, name, year_level, and program.
Copy code

DROP TABLE students;


DROP Syntax and Examples
This command deletes the students table and all its
● DROP DATABASE: Deletes an entire
rows.
database instance.

Syntax:
DML
sql
DML manages data stored in the database. Common
Copy code
operations include:
DROP DATABASE database_name;
● SELECT
● INSERT
● UPDATE
Example: ● DELETE

sql

Copy code

DROP DATABASE school; What is Multithreading?

● Multithreading is similar to multi-processing.


● A multi-processing operating system can
run several processes at the same time:
AOOP

○ Each process has its own 1. Applications with distinct tasks that can
address/memory space. be performed independently:
○ The OS's scheduler decides when ○ Any kind of application that requires
each process is executed. such division of tasks.
○ Only one process is actually 2. Applications with a GUI:
executing at any given time, but the ○ Threads dedicated to the GUI can
system appears to be running delegate user requests to other
several programs simultaneously. threads.
● In a multithreaded application: ○ The GUI remains responsive to the
○ There are several points of user while requests are being
execution within the same memory processed.
space. 3. Applications requiring asynchronous
○ Each point of execution is called a response:
thread. ○ Network-based applications:
○ Threads share access to memory. ■ Data can arrive from the
network at any time.
■ In a single-threaded
system, data is queued
Why Use Multithreading? until the thread can read it.
■ In a multithreaded system,
● In a single-threaded application:
a thread can be dedicated
○ One thread of execution must do
to listening for data and
everything.
processing it or delegating
○ Tasks are performed when the
its processing to another
thread can get to them.
thread.
○ A single task requiring a lot of
processing can make the entire
application appear "sluggish" or
unresponsive. How Does It All Work?
● In a multithreaded application:
○ Each task can be performed by a ● Threads have their own "context":
separate thread. ○ Includes virtual registers and their
○ A long process in one thread does own calling stack.
not make the entire application wait ● The scheduler decides which thread
for it to finish. executes:
○ On a system with multiple ○ The VM may use its own scheduler
processors, the OS may execute or the system's scheduler.
separate threads simultaneously on ○ Scheduler maintains a list of ready
separate processors. threads (run queue) and threads
waiting for input (wait queue).
○ Threads have priorities, and the
scheduler typically schedules the
What Kind of Applications Use Multithreading?
AOOP

highest-priority threads in the run ● The run() method of a thread class


queue. executes when the thread's start()
method is invoked.
● The thread terminates when the run
Java Thread Support method terminates.
● To prevent termination, the run method
● Few programming languages directly must not end (commonly achieved with an
support threading. endless loop).
○ Many rely on cumbersome add-ons.
● The Java Virtual Machine (JVM) has its
own runtime threads:
○ Example: Garbage collection. Creating Threads
● Thread class:
● Subclassing Thread:
○ Represents threads and provides
○ Override the run() method.
control methods (start,
○ Call the start() method to
interrupt, sleep, etc.).
execute.
● When an application executes, the main
○ Not recommended since a class
method runs on a single thread. Additional
cannot inherit from another class if
threads must be created if required.
it already inherits from Thread.
● Using Runnable Interface:
○ Provides the run() method.
Thread States ○ Pass a Runnable object to the
Thread constructor.
Threads can be in one of four states: ○ When start() is called, the
1. Created thread runs the run() method of
2. Running the Runnable object.
3. Blocked
4. Dead

Thread states change due to:


Terminating Threads
● Control methods (start, sleep,
yield, wait, notify). ● Deprecated stop() method:
● Termination of the run method. ○ Unsafe as it may cause deadlocks.
● Recommended method:
○ Use a boolean variable to indicate
whether the thread should continue.
How Does a Thread Run?
○ The run() method checks the
variable and terminates if
necessary.
AOOP

○ Threads must acquire the lock


before entering synchronized
Multiple Threads sections.

● To create multiple threads, create multiple


instances of the Runnable class and call
start() on each. Synchronization Issues
● Maximum threads depend on platform and
available RAM (approximately 2 MB per 1. Thread-safe access:
thread). ○ Synchronize access to critical data.
2. Performance overhead:
○ Synchronization adds delays as
threads wait for locks.
Thread Priorities ○ Minimize synchronized sections or
use synchronized blocks instead of
● Threads have a priority between 1 and 10 entire methods.
(default is 5).
● Scheduler executes higher-priority threads
first.
● Modern OSes use thread aging to balance
priorities dynamically.

Yield and Sleep

● Yield: Suggests that the scheduler switch to


another thread.
● Sleep: Moves the thread to the wait queue
for a specified time.

Synchronization

● Why synchronize?
○ Prevents data corruption when
multiple threads access shared
data.
● How to synchronize:
○ Use the synchronized
keyword for methods or blocks.

You might also like