oOP Flashcards
are the variables that each object (each instance of a class) has as part of itself.
instance variables
means they are accessible only to the object itself.
private,
returns a reference to the object it constructs
constructor
Parameter names follow the same rules as the other names you might pick for a program. Names of this type are called
this
is a variable that holds part of the state of an object.
instance variable
In Java, a characteristic of a class that is not part of its objects
static.
Can the static main() method invoke other static methods of the class?
Yes. Static methods can call other static methods.
Will parameter passing be the same as with the instance methods of the previous chapters?
Yes. Static methods pass data to other static methods through parameters.
The program keeps looping until the user signals the end. What type of loop is this?
A sentinel controlled loop.
What is the parameter of the method that calculates factorial?
The value of N, an integer
What is the return value of the method that calculates factorial?
The factorial of N.
is an incomplete version of a method that is used in place of the full method. Extremely useful when developing programs.
stub
let you test individual methods. This can be very useful.
unit testing
is one whose data does not change.
immutable object
The arguments in a method call are often referred to as ____.
actual parameters
A(n) ____ method is a method that creates and initializes class objects.
constructor
____ is the encapsulation of method details within a class.
Implementation hiding
When a variable ceases to exist at the end of a method, programmers say the variable____.
goes out of scope
Method names that begin with “____” and “set” are very typical.
get
Public classes are accessible by all objects, which means that public classes can be
extend or/and used as a basis
Assigning ____to a field means that no other classes can access the field’s values.
private access
Methods used with object instantiations are called ____ methods.
instance
Methods that retrieve values are called ____ methods.
accessors
A(n) unique identifier is most likely used as a(n) ____ key in a database.
primary
For ease in locating class methods, many programmers store them in ____ order.
alphabetical
A(n) ____ constructor is one that requires no arguments.
default
A(n) ____ is a program module that contains a series of statements that carry out a task.
method
The method ____ is the first line of a method.
declaration
executes first in an application, no matter where you physically place it within its class.
main method
Data items you use in a call to a method are called ____.
arguments
A(n) ____ variable is known only within the boundaries of the method.
local
A(n) _____ causes a value to be sent from a called method back to the calling method.
return statement
An object is a(n) ____ of a class.
member
for a class, you no longer have access to the automatically created version.
when you write a constructor
You are ____ required to write a constructor method for a class.
never
To execute a method, you ____________________ it from another method.
invoke
Access specifiers are also called access ____________________.
modifiers
When you think in an object-oriented manner, everything is a(n) ____________________.
object
A(n) ___________ is a number appended to a field, typically an ID number or account number.
check-digit
so that other classes can instantiate objects that belong to the class.
You declare constructors to be object
First line of a method
header
Combination of the method name and the number, types, and order of arguments
methods signature
Included within the method declaration parentheses
parameter type
Are called class methods
static methods
Variables in the method declaration that accept the values from the actual parameters
formal parameter
Used to return value back to the calling method
return statement
Data components of a class
instance variables
Application or class that instantiates objects of another prewritten class
class user
Variables you declare within a class, but outside of any method
data fields
a method that allows you to create objects from a class. You call the constructor by using the keyboard new, followed by the name of the class followed by any necessary parameter
Constructor
A method that allows you to create objects from a class. you call the constructor by using the keyword new followed by the name of the class, followed by any necessary parameter
Constructor
a method name followed by parenthesis Ex. calculate()
call
Value passed to a method
Argument
part of the method that receives data must the same type and in the same order as it is stated in the method declaration
Method parameter
Blueprint in which objects are created
class
initial requirement of the method. It is normally stated as part of the methods documentation.
Precondition
Creating a new class based on a previously defined class
Inheritance
Class that inherits from another class
Subclass
Has a name, parameter, and a return type, but no code in the body of the method.
Abstract Method
Class that contains one or more abstract methods. Abstract classes may not be instantiate, and require subclasses to provide implementation for the abstract methods
Abstract Class
Methods that stores a value in a field or in some other way changes the value of a field
Mutators
Does not return a value
Void Method
Cause a methods to end and sends a value back to the statement that called the methods
return
Two or more methods with the same name but different parameter types, order, and/ or number of parameters. When overloaded methods are called, the compiler decides which one to use based on the number, order, and type of parameter.
Method overloading
Methods that gets a value from a class’s field, but does not change it
Accessors
Return the character at the specified index
char charAt(int index)
Returns number of characters in the string
int Length()
Return true if this string contains the same characters as str and false if it doesn’t. Ignores lower and upper case
boolean equalIgnore Case(String str)
Returns position of the first character in the first occurrence of str in this string
int indexOf(String str)
Returns new string that is the same except all letters are in lowercase
String toLower Case{}
returns - –uppercase
String toUpperCase()
Adds the specified string to the end of original string
-String concat(String str)
A(n) __ is a program module that contains a series of statements that carry out a task.
Methods
the method__is the first line of a methods
Declaration
Executes first in application no matter where you physically place it within its class
Main Method
Data items you use in a call to a method are called
Arguments
____is the encapsulation of method details within a class
Implementation Hiding
A(n)___variable is known only within the boundaries of the methods
- Local
When a variable cease to exist st the end of a method, programmers say the variable____
-Goes out of scope
The arguments in a methods call are often referred to as____
Actual Parameter
A(n)____causes a value to be ssent from a called method back to the calling metho
return statement
an object is a(n) ___ of a class
-Member
Methods names that begins with “___” and “set” are very typical
Public classes are accessible by all objects, which means that public classes can be
Extend or / and used as a basis
Assigning ____ to a field means that no other classes can access the field’s values
Private access
Methods used with objects intantiations are called _____methods
Instance
Methods that retrieve values are called _______methods
Accessors
A(n) unique identifier is most likely used as a(n) ____key in a database
Primary
A(n) _____methods is a methods that creates and initializes class objects
Constructor
For ease in locating class methods, many programmers stores them in ____ order.
Alphabetical
A(n) ___ constructor is one that requires no arguments
Default
You are ___required to write a constructor methods for a class
Never
to execute a method, you ____it from another methods
Invoke
Access specifiers are also called access___
Modifiers
When you think in an object-oriented manner, everything is a (n)____
Object
A(n) ____ is a number appended to a field, typically an ID number or account number
Check digit
So that other classes can instantiate that belong to the class
You declare constructors to be object
First line of method
Header
Combination of the methods name and the number, types, and order of arguments
Methods signature
included with the methods declaration parantheses
Parameter type
Are called class methods
Static methods
variables in the methods declaration that accept the values from the actual parameters
Formal statement
used to return value back to calling method
return statement
data components of a class
instance variable
application or class that instantiate objects of another prewitten class
class user
variables you declare within a class, but outside of any methods
data fields
a group of characters separated by spaces
token
preparing a disk file for reading
operating file
used to send characters to a text file
PrintWriter
data that is supplied to a method just before it starts running.
parameter
ArrayDemo.
3 Fundamental Features of OOP
1) encapsulation
2) inheritance
3) polymorphism
an object-oriented technique in which the internal details of an object are “hidden” in order to simplify their use and reuse
encapsulation
a relationship between two classes in which one class ( the subclass) inherits all of the properties and capabilities of another ( the superclass)
inheritance
declaring a variable to have one type while it refers to an object of a subclass of that type
polymorphism
a means of determining what aspects of a given problem can be viewed as objects in an eventual object-oriented design
object-oriented analysis
a software design method that models the characteristics of abstract or real objects using classes and objects.
object-oriented design
a programming technique that features objects, classes, encapsulation, interfaces, polymorphism, and inheritance.
object-oriented programming
degree to which responsibilities of a given component are a meaningful unit
cohesion
greater cohesion reflects better design; less coupling reflects better design.
cohesion vs coupling
degree to which one component must directly access the data of another component.
coupling
sets of responsibilities
roles
they are the same when a given role is always played by the same object type
roles vs. objects
3 Means of Object-Object Access
1) parameter passing
2) object declared in method
3) object is a member of another object through composition [subtype]
describes an inheritance relationship between two classes
is-a relationship
the relationship demonstrated by a class that contains another class
has-a relationship
implemented-in-terms-of relationship
uses-a relationship
known as “class variables”, because there is only one unique value for all the objects of that same class
static members
special reference that refers to the object in which it is used
this
special reference that refers to the superclass of the object in which it is used
super
Determine whether a subclass is a subtype
Liskov Substitution Principle
no outside classes can use this data field (except descendant classes), defeats the purpose of OOP
protected access
type cast to the type of an ancestor class; usually done automatically
upcasting
type cast to the type of a descendant class; may cause runtime errors
downcasting
UML diagram that captures all actors (an individual or another system) and use cases involved with a system or product; Used to model user requirements
use case diagram
a visual representation of the relationships between classes
class diagram
represents real-world whole-part relationships
e.g., an engine is a part of a car
composition
represents a class as a collection or container of other classes
aggregation
used to show that some UML element or a set of elements requires, needs or depends on other model elements for specification or implementation
dependency
minimum and maximum number of occurrences of one object for a single occurrence of related object
multiplicity
signifies the role that a participating entity type plays in each relationship instance. In some cases the same entity type participates more than once in a relationship type in different roles.
role name
a type of diagram that shows objects participating in interactions and the messages exchanged between them.
sequence diagram
this pattern ensures you have at most one instance of a class in your application
singleton pattern
the design pattern that provides a way to access the elements of an aggregate object sequentially without exposing the underlying representation.
iterator pattern
the design pattern that translates one interface for a class into a compatible interface
adapter pattern
- simplifies the interface of a group of classes,
- hides the complexity of one or more classes behind a clean, well-lit facade,
- makes an interface simple.
- provides a unified interface to a set of interfaces in a subsystem
facade pattern
1) Determines the requirements of a system
2) Focus on what needs to be done (not how)
3) Carried out by a team of analysts
4) End product is a conceptual model
Object-Oriented Analysis Stage
1) Major subsystems should be loosely coupled, allowing independence
2) Many-to-many relationships require an additional class
3) Avoid exposing details of business logic implementation to the user interface
4) Avoid exporting mutable objects
5) Avoid importing objects of an internal type in the system
5 Design Principles
aka “Single Dot Rule”
Long lists of method calls are attributed to bad design
Law of Demeter
Integer class is an example of Decorator pattern.
(a) True
(b) False
(a) True
In which of the following pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes?
(a) Factory Pattern
(b) Abstract Factory Pattern
(c) Singleton Pattern
(b) Abstract Factory Pattern
Which of the following describes the Structural pattern correctly?
(a) This type of patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator
(b) This type of patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
(c) This type of pattern are specifically concerned with communication between objects.
(b) This type of patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
Which of the following is the correct list of entities of MVC pattern?
(a) Middle, Viewer, Controller
(b) Model, View, Control
(c) Model, Viewer, Control
(d) Model, View, Controller
(d) Model, View, Controller
Define a family of algorithms, encapsulate each one, and make them interchangeable. It lets the algorithm vary independently from clients that use it.
(a) Decorator
(b) Composite
(c) Strategy
(c) Strategy
Which method must be defined by a class implementing the java.lang.Runnable interface?
(a) public void start()
(b) void notify()
(c) void run()
(d) none of these
(c) void run()
wait(), notify() & notifyAll() are methods of which class or interface? (a) Object
(b) Thread class
(c) Runnable interface
(d) None
(a) Object
When start() method is called on thread it enters ______ state.
(a) running
(b) new
(c) waiting
(d) runnable
(d) runnable
What are the different ways of creating thread?
(a) Extending Thread class.
(b) Implementing Runnable interface
(c) Using Thread Executor framework ( thread pool)
(d) All of the above
(e) None of the above
(d) All of the above
What is race condition in Java
(a) Two threads try to execute earlier form sort of race.
(b) Two or more threads can access shared data and they try to change it at the same time
(c) Race condition doesn’t allow two threads to access same resource
(b) Two or more threads can access shared data and they try to change it at the same time
JUnit tests can be run automatically, and they check their own results and provide immediate feedback.
(a) True
(b) False
(a) True
Which of the following annotation causes that Junit method run once after all tests have finished?
(a) @BeforeClass
(b) @AfterClass
(c) @Test
(d) @After
(b) @AfterClass
What is the base class of nearly all Swing classes?
(a) Object
(b) Component
(c) JComponent
(d) Swing
(c) JComponent
What is a container object in GUI programming?
(a) A container is another name for an array or vector.
(b) A container is any class that is made up of other classes.
(c) A container is a primitive variable that contains the actual data.
(d) A container is an object like a frame that has other GUI components placed inside of it.
(d) A container is an object like a frame that has other GUI components placed inside of it.
When you create a JFame object it is not automatically displayed. (a) True
(b) False
(a) True
Which of the following are false about Collections and Collection? (a) Collections is a utility class
(b) Collection is an interface to Set and List
(c) Collections is a special type of collection which holds Set of collections
(d) Both Collections and Collection entity belongs to java.util package.
(c) Collections is a special type of collection which holds Set of collections
Which of the following is false about collections in Java?
(a) List, Map and Set are interfaces.
(b) java.util is the default package for collection classes.
(c) Collection interface is the base interface.
(d) List, Map and Set are abstract classes
(d) List, Map and Set are abstract classes
What is it called when a program is written to respond to the button clicks, menu selections, and other actions of the user in whatever order the user does them?
(a) Event-driven programming.
(b) Action-driven programming.
(c) User-driven programming.
(d) Mouse-driven programming
(a) Event-driven programming.
What is the name for a method that responds to events?
(a) A container method.
(b) A listener method.
(c) A snoop method.
(d) An application method
(b) A listener method.
. Which of the following methods can be used to draw the outline of a square within a java.awt.Component object?
- fillRect()
- drawLine()
- drawRect()
- drawString()
- drawPolygon()
(a) 2 and 3
(b) 1,2, and 3
(c) 2,3, and 5
(d) 3,4, and 5.
(c) 2,3, and 5
A class is divided into which of these compartments?
a. Name Compartment
b. Attribute Compartment
c. Operation Compartment
d. All of the above
d. All of the above
What is multiplicity for an association?
a. The multiplicity at the target class end of an association is the number of instances that can be associated with a single instance of source class
b. The multiplicity at the target class end of an association is the number of instances that can be associated with a number instance of source class
c. a) and b)
d. None of the above
a. The multiplicity at the target class end of an association is the number of instances that can be associated with a single instance of source class
Which of these are the heuristics?
a. Name classes, attributes, and roles with noun phrases
b. Name operations and associations with verb phrases
c. Stick to binary associations
d. All of the above
d. All of the above
Use case descriptions consists of interaction among which of the following?
a. Products
b. Actors
c. Products & Actors
d. Usecases
c. Products & Actor
Use case description contents includes? a. Use case name and number b. Actors c. Stakeholder and needs d. All the above
d. All the above
Use case diagram and sequence diagram are considered as types of
a. non-behavioral diagrams
b. behavioral diagrams
c. non structural diagrams
d. structural diagrams
b. behavioral diagrams
Which of the following is NOT a tool that can be used for discovering class behaviors and responsibilities? a. CRC cards b. interface c. sequence diagram d. none of the above
b. interface
In which of the following relationships, does the whole have a responsibility for the part, and is usually shown with a filled-in diamond?
a. Whole/part
b. Composition
c. Aggregation
d. Association
b. Composition
Which diagram models the life cycle of a single object?
a. Sequence diagram
b. Use case diagram
c. State machine diagram
d. None of the above
c. State machine diagram
Indicate if the statement is true or false based on the UML diagram below: “This web service is used by any number of users” a. True
b. False
a. True
Which of these can be used to fully abstract a class from its implementation?
(a) Objects
(b) Packages
(c) Interfaces
(d) None of the Mentioned.
(c) Interfaces
In Java, a class may have _superclass(es) and implement ___interface(s)
(a) only one / only one
(b) multiple / only one
(c) only one / multiple
(d) multiple / multiple
(c) only one / multiple
What is the correct syntax for defining a new class Parakeet based on the superclass Bird?
(a) class Parakeet is-a Bird{ }
(b) class Bird defines Parakeet{ }
(c) class Bird has-a Parakeet{ }
(d) class Parakeet extends Bird{ }
(d) class Parakeet extends Bird{ }
The private members of a superclass can be accessed by a subclass.
(a) True
(b) False
(b) False
You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass.
(a) True
(b) False
(a) True
The method toString() is a public member of the class ____.
(a) Object
(b) String
(c) Writer
(d) Output
(a) Object
An abstract method ____.
(a) is any method in the abstract class
(b) cannot be inherited
(c) has no body
(d) is found in a subclass and overrides methods in a superclass using the reserved word abstract
(c) has no bod
If class Dog has a subclass Retriever, which of the following is true?
(a) Because of single inheritance, Dog can have no other subclasses.
(b) Because of single inheritance, Retriever can extend no other class except Dog.
(c) The relationship between these classes implies that Dog “is-a” Retriever.
(d) The relationship between these classes implies that Retriever “has-a” Dog.
(b) Because of single inheritance, Retriever can extend no other class except Dog.
Which of the following statement about Java is FALSE?
(a) All objects are dynamically allocated.
(b) Every function (method) must belong to a class.
(c) It uses garbage collection to deallocate dynamically allocated objects.
(d) ArrayList elements can be primitive types or objects.
(d) ArrayList elements can be primitive types or objects.
In Java, you can automatically make a reference variable of a subclass type point to an object of its superclass.
(a) True
(b) False
(b) False
Reuse of code (e.g. Visual Basic, JavaBean)
Software Components
Reuse of “plug-in” code (e.g. Java Swing)
Frameworks
Reuse of Design
Design Paterns
is a sequence of values of the same type. They can hold both primitives and objects.
array
What field returns the number of elements in an array?
The length field. It is an instance variable of the array.
Syntax for array element access
arrayReference[index]
Ex. data[2]
Syntax for initializing an array
typeName[] arrayReference = new typeName[length]
Ex. double[] data = new double[10];
How do you access the last index of an array?
arrayReference[length - 1]
Initialize an integer array named primes with five values.
int[] primes = new int[] { 2, 3, 5, 7, 11 };
using a method on an uninitialized object will cause a ___________. Unchecked.
NullPointerException
Each object is a distinct individual
Identity
has a various properties, which might change
State
It can do thing and can have things done to is
Behavior
A description of a kind of object
Class
Creating an object
Instantiation
a characteristic of a class that is not shared by its objects
Static
the method that belongs to a class definition
Static Method
this means that after construction, they cannot be altered
String Objects are immutable
Is constructed out a block of main memory and hence has a unique location
Object
Is constructed out a block of main memory and hence has a unique location
Object
Describe the location in memory of a particular object
Object reference
Does not contain the actual object, just how to find it.
Reference Variable
Is the correct term for objects that are not referred to by any referred to by any reference variable
Garbage
Reclaims the last objects so that the memory they are made of can be used again.
Garbage collector
are sometimes called variable
field
Is a request to run a method of an object
Method call
Used to specify which object, and what method of that object run
Dot notation
the programmer ask for values to be converted with a type cast
Explicitly
If the compiler can make the conversion without loss of information, or with only a small loss of precision, it will automatically do so.
Implicitly
If the compiler can make the conversion without loss of information, or with only a small loss of precision, it will automatically do so.
Implicitly
Means that some of the less significant digit may become zeros, but the most important digits and the size of the size of the number will remain
Less of precision
Are very useful and are frequently used
String Object
Is a special value that means “no object”
Null
A string object that contains no characters is still an object
Empty String
Sometimes immutable objects are called
write-once
the concat method performs
String concatenation
When an operator such as + changes meaning depending on its argument, it is said to be
Overloaded
the stage where the compiled program is running
Run Time
used to hand back a value to the caller
Return statement
is the type of value that method hands back to the caller of the method.
Return type
is automatically supplied by the java compiler
Default Constructor
is a list of values and their types
Parameter list
class that contains main()
Driver
is a variable that holds part of the state of an object
Instance Variable
Variable that are object(each instance of a class) has a part of itself
Instance Variable
is the type of one item of data that will be handed to the constructor
Data Type
Is a name that is used for that item of data
Parameter Name
to show when an identifier refers to an objects instance variable
This
is an identifier picked by the programmer
Method name
is the method name and parameter list
Signiture of a method
a method that changes the state of an object
Mutator / setter
a method that returns a value from an instance variable
Access method/ getter
Decide what checking account object do and what data they hold
requirement analysis
the compiler puts in a toString() method even if you did not write
Inheriance
recall that there can be more than one reference to a given object. Each reference is called
alias
using a method on an uninitialized object will cause a ___________. Unchecked.
NullPointerException
situation when use to exceptions.
Accessing Data Files, User input
These exceptions are always Unchecked
Runtime Exceptions
Class is in the java.lang package
Throwable
Class extends the Object class
Throwable
Class implements the Serializable Inteface
Throwable
Used for launching an exception.
throw new
User has entered invalid data
Exceptions Best Practice
A file that needs to be opened cannot be found
Exceptions Best Practice
Network connection has been lost in the middle of communication.
Exceptions Best Practice
JVM has run out of memory
Exceptions Best Practice
Class includes the bytesTransferred filed that provides information on how many bytes were transferred successfully before the interruption occurred
InterruptedIOException
This classes can be thrown
Exception()
This classes can be thrown
Throwable()
This classes can be thrown
Error()
This classes can be thrown
RuntimeException()
Has the fewest subclasses when compared to Exception and RuntimeException Classes.
Error Class
SuperClass of Error Exceptions, Subclass of VirtualMachineError. Constructors accept String argument or no argument.
Internal Error
SuperClass of Error Exceptions, Subclass of VirtualMachineError. Constructors accept String argument or no argument.
OutOfMemoryError
SuperClass of Error Exceptions, Subclass of VirtualMachineError. Constructors accept String argument or no argument.
StackOverflowError
SuperClass of Error Exceptions, Subclass of VirtualMachineError. Constructors accept String argument or no argument.
UnknownError
This class does not have a no argument constructor
IIOException
Used for logging unexpected behavior
Java Exception Mechanism
Used to determine what to do when something unexpected happens.
Java Exception Mechanism
Standard Java exception class
java.lang.SecurtiyException
Standard Java exception class
java.io.FileNotFoundException
Improves code because error handling code is clearly separated from main program logic.
Java Exception Mechanism
Allows creation of new exceptions that are custom to a particular application domain.
Java Exception Mechanism
Allows creation of new exceptions that are custom to a particular application domain.
Java Exception Mechanism
Can declare that it throws a checked exception
Main Method Exception
Exception can throw instances of any subclass of that exception class.
Exception Classes
Can not be reached in the catch block.
try block variables
IllegalStateException
Application
IllegalArgumentException
Application
ClassCastException
NullPointerException
SecurityException
JVM
What is the process of defining more than one method in a class differentiated by parameters?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of these
b
Which of these can be used to diffrentiate two or more methods having same name?
a) Parameters data type
b) Number of parameters
c) Return type of method
d) All of the mentioned
d
What is the output of this program?
class box { int width; int height; int length; int volume; void volume(int height, int length, int width) { volume = width height length; } } class Prameterized_method{ public static void main(String args[]) { box obj = new box(); obj.height = 1; obj.length = 5; obj.width = 5; obj.volume(3, 2, 1); System.out.println(obj.volume); } } a) 0 b) 1 c) 6 d) 25
c
Which of these data tupe can be used for a method having a return statement in it?
a) void
b) int
c) float
d) All of the mentioned
d
What is the output of this program?
class equality { int x; int y; boolean isequal(){ return(x == y); } } class Output { public static void main(String args[]) { equality obj = new equality(); obj.x = 5; obj.y = 5; System.out.println(obj.isequal); } } a) false b) true c) 0 d) 1
b
What is the output of this program?
class box { int width; int height; int length; int volume; void volume() { volume = width height length; } void volume(int x) { volume = x; } } class Output { public static void main(String args[]) { box obj = new box(); obj.height = 1; obj.length = 5; obj.width = 5; obj.volume(5); System.out.println(obj.volume); } } a) 0 b) 5 c) 25 d) 26
b
What is the output of this program? class Output { static void main(String args[]) { int x , y = 1; x = 10; if(x != 10 && x / 0 == 0) System.out.println(y); else System.out.println(++y); } } a) 1 b) 2 c) Runtime Error d) Compilation Error
d
What is the output of this program?
class area { int width; int length; int volume; area() { width = 5; length = 6; } void volume() { volume = width height length; } } class cons_method { public static void main(String args[]) { area obj = new area(); obj.volume(); System.out.println(obj.volume); } } a) 0 b) 1 c) 25 d) 30
d
When does Exceptions in Java arises in code sequence?
a) Run Time
b) Compilation Time
c) Can Occur Any Time
d) None of the mentioned
a
Which of these keywords is not a part of exception handling?
a) try
b) finally
c) thrown
d) catch
c
Which of these keywords must be used to monitor for exceptions?
a) try
b) finally
c) throw
d) catch
a
Which of these keywords must be used to handle the exception thrown by try block in some rational manner?
a) try
b) finally
c) throw
d) catch
d
Which of these keywords is used to manually throw an exception?
a) try
b) finally
c) throw
d) catch
c
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } } a) Hello b) World c) HelloWorld d) Hello World
b
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } } } a) A b) B c) Compilation Error d) Runtime Error
b
} } a) Hello b) World c) HelloWorld d) Hello World b
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } } } a) A b) B c) Compilation Error d) Runtime Error b
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } finally { System.out.print("C"); } } } a) A b) B c) AC d) BC
d
} } a) Hello b) World c) HelloWorld d) Hello World b
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } } } a) A b) B c) Compilation Error d) Runtime Error b
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { int a, b; b = 0; a = 5 / b; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } finally { System.out.print("C"); } } } a) A b) B c) AC d) BC
d
What is the output of this program? class exception_handling { public static void main(String args[]) { try { int i, sum; sum = 10; for (i = -1; i < 3 ;++i) sum = (sum / i); } catch(ArithmeticException e) { System.out.print("0"); } System.out.print(sum); } } a) 0 b) 05 c) Compilation Error d) Runtime Error
c
What is the output of this program?
class exception_handling { public static void main(String args[]) { try { int i, sum; sum = 10; for (i = -1; i < 3 ;++i) { sum = (sum / i); System.out.print(i); } } catch(ArithmeticException e) { System.out.print("0"); } } } a) -1 b) 0 c) -10 d) -101
c
Which of these is a super class of all exceptional type classes?
a) String
b) RuntimeExceptions
c) Throwable
d) Cachable
c
Which of these class is related to all the exceptions that can be caught by using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
b
Recu____ the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.
Recu____ may be a bit difficult to understand. The best way to figure out how it works is to experiment with it
Java recursion
Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers:
Use recursion to add all of the numbers up to 10.
public class MyClass { public static void main(String[] args) { int result = sum(10); System.out.println(result); } public static int sum(int k) { if (k > 0) { return k + sum(k - 1); } else { return 0; } } }
Recursion continued
Example Explained
When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and returns the result. When k becomes 0, the function just returns 0. When running, the program follows these steps:
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
…
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Since the function does not call itself when k is 0, the program stops there and returns the result.
Recursion continued part 2
Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself. Every recursive function should have a halting condition, which is the condition where the function stops calling itself. In the previous example, the halting condition is when the parameter k becomes 0.
It is helpful to see a variety of different examples to better understand the concept. In this example, the function adds a range of numbers between a start and an end. The halting condition for this recursive function is when end is not greater than start:
Halting condition
Use recursion to add all of the numbers between 5 to 10.
public class MyClass { public static void main(String[] args) { int result = sum(5, 10); System.out.println(result); } public static int sum(int start, int end) { if (end > start) { return end + sum(start, end - 1); } else { return end; } } }
Halting condition continued
the ___ package contains frequently used classes and is implicity imported into java program.
java.lang
defienes the circumstances under which a class can be accessed and the other classes that have the right to use a class
access specifier
an indefinite loop
event - controlled
requires three expressions separated with a question mark and a colon
conditional operator
describes the feature of language that allows the same word to be interpreted correctly in different situations based on the content
polymorphism
another name for a nonstatic member class
inner class
statement is the decision structure you use when you need to take other of the two possible course of action
dual alternative if
programs that are embedded in a web page
java applets
is the type to which all operands in an expression are converted so that they are compatible with each other
unifying type
refers to the order in which values are used with operators
associativity
are also called modules, methods, function, and subroutines
producers
the reference to an object that is passed to any object’s nonstatic method
this reference
you can use ___ arguments to initialize field values, but you can also use arguments for any other purpose
constructor
is the process of ensuring that a value falls within a specified range
validating data
loop_____ is the technique of combining two loops into one
fusion
the order of the conditional expressions in the following is most important within a(n) ____ loop
nested
a(n)____ loop is an indefinite loop in which the number of execution is determined by user actions
event - controlled
a(n) ____ loop is one that performs no actions other than looping
do-nothing
one execution of any loop
iteration
the modulus operator (%) is a _____operator
Binary
a value that stops the loop
sentinel
a loop controlled by the user is a type of _____loop
incrementing
is the first input statement prior to a loop that will execute a subsequent input statement for the same variable
Priming read
Before entering a loop, the first input statement, or ___, is retrieved
Priming read
the prefix that is a ____operator
Unary
the process of repeating increasing a value by some amount
Accumulating
is used for mean in java
final
is used as a return value to flag an error
ERRORFLAG
is data that is supplied to a method just before it starts running.
Parameter
when they have the same name but different types or numbers of parameters. The compiler will inspect the parameters of a method call and pick the method that best matches the parameter list.
overloaded methods
Do you think that this method should print out an error message when it detects an error?
yes
what types of data can arrays hold in their cells?
Any type of data at all:
What is the name of the parameter passing mechanism that Java uses?
Call by Value.
the programmer ask for values to be converted with a type cast
Explicitly
if the compiler can make the conversion without less of information or with only a small less of precision, it will automatically do so.
Implicitly
means that some of the less significant digit may become zeros but the most important digits and the size of the size of the number will remain
Less precision
is a name that is used for that item of data
parameter name
is the method name and parameter list
signiture of a method
a method that changes the state of an object
Mutator method
a method that changes the state of an object
Mutator/Setter
a method that returns a value from an instance variable
Access method / Getter
when an object’s instance variables are visible only to the subject’s own methods.
Encapsulation
can be used only by the other methods of the objects
Private Method
a group of related classes
Package
a group of related classes
Package
is a value that is sent to a method when it is called
Parameter
is a name used for a class a variable or a parameter
Identifier
are bound to an actual value only as long as their method is active
Formal Parameter
the section of source code that can see the parameter
Scope of a formal parameter
is a variable that is declared inside of the body of a method
Local Variable
short from where it is declared and ends at the end of the body of a method
Scope of local variable
the identifier used in a method to sand for the value that will be passed into the method by a caller
Formal Parameter
the actual value that is passed
Actual Parameter
the type of parameter passing that java uses
call by value
is part of an object of the type describe by the class
Instance Variable
a variable whos scope extends over the entire program
Global Variable
is an object that is used to share a list of values
arrays
an array is made out of a contiguous block of memory that is divided into a number of
cell
an expression such as data[3]
Subscripted variable
a java program is running, each time an array index is used it is checked to be sure that it is ok
Bounds Checking
The words on the command line after the name of the program
command line arguments
an array that uses a single substription
One dimensional array
requires two indexes to access values
two dimensional array
a systematic procedure used to compute something
Algorithm
How can a program tell how many command line arguments it has been given?
Use args.length
Are the data in a list always processed in order?
No.
Can the linear search algorithm be used on a partially full array?
Yes, of course. You need to watch out for nulls, though.
used to postpone the handling of a checked exception
throws
is used to invoke an exception explicitly.
throw
Which exceptions needs to be declared
only checked exception
Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
a) Error
Which of these handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system
a) Default handler
Which of these keywords is used to generate an exception explicitly?
a) try
b) finally
c) throw
d) catch
c) throw
Which of these class is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw
c) Throwable
Which of these class is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw
c) Throwable
Which of these class is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw
c) Throwable
Which of these operator is used to generate an instance of an exception than can be thrown by using throw?
a) new
b) malloc
c) alloc
d) thrown
a) new
Which of these handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system
a) Default handler
Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch
c) throws
a sequence of data
- two kinds of Streams = InPutStream, OutPutStream
Stream
is used to read data from a source.
InPutStream
is used for writing data to a destination.
OutPutStream
perform input and output for 16-bit unicode
- most frequently used classes are, FileReader and FileWriter.
- Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.
Character Streams
How to find the number of elements in:
a) an Array
b) an ArrayList
c) a String
a) .length
b) .size()
c) .length()
stores a reference to the array. Copying the variable yields a second reference to the same array.
array variable
Syntax of copying an array.
double[] prices = (double[]) data.clone();
This is used to output the error data produced by the user’s program and usually a computer screen is used for standard error stream and represented as System.err.
Standard Streams : Standard Error
Two different arrays that should be combined into one array containing objects.
parallel arrays