Object Oriented Programming

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Overriding can be defined as...

Allowing a subclass class to overwrite and provide a specific implementation of a method that is already defined by one of its super-classes

A _________ is a data structure that has a root node that points to at most 2 child nodes that are referred to as "left child", "right child". Below is an image of the data structure.

Binary Tree

Expressions

Combine variables, constants, methods returning values using operator Taking variables and applying operators to them; contains operations, numbers, and/or variables.

String Concatenation

Combines two java string fields using a "+" indicator The plus symbol (+), when used with Strings, stands for String concatenation. It merges two Strings together into one.

Consider the following class: public class Person { public String first_name; public String last_name; private int age; public Person(){ this.first_name = "Jack"; this.last_name = "Ryan"; this.age = 27; } public String toString(){ return this.first_name + " " + this.last_name + " " + this.age; } } What would be the output of the below program using the Person class? public class Main{ public static void main(String[] args){ Person henry = new Person(); henry.first_name = "Henry"; henry.last_name = "Schmeding"; henry.age = 30; System.out.println(henry.toString()); } } A. Henry Schmeding 27 B. Henry Schmeding 30 C. Compilation error would occur. Cannot access the age attribute outside of the Person class D. Jack Ryan 27

Compilation error would occur. Cannot access the age attribute outside of the Person class

Compiler vs. Interpreter

Compiler: program that converts source code into machine language instructions that can be processed by the computer; the resulting program runs faster than the interpreter; *source code must run through a compilation step before the program can be run* Interpreter: reads through & converts code to instructions one line at a time while it's running and sends it to the processor; if you have a code that changes frequently, it's better to use an interpreter because it has more flexibility; when you need to change a code using a compiler you have to shut the program down, implement the change, and then start; *capable of running source code line by line without going through a compilation step*

Casting methods

Convert one number format type to another. ex: int to float

What would be the console output given for the below program? public class Scratch { public static void main(String[] args) { String string_number = "twenty"; int number = 0; try { number = Integer.parseInt(string_number); } catch (Exception e) { System.out.println("Could not parse string_number"); return; } System.out.println("number is equal to " + number); } } A. number is equal to twenty B. number is equal to 0 C. Could not parse string_number D. number is equal to 20

Could not parse string_number

Throwing an exception

Creating an exception object and handing it to the runtime system; a program's method of reporting an exception and an attempt to explain the exception

Boolean operators symbol: ( *==* )

Equal to

An abstract class can be instantiated using the new keyword. True/False

False

The below code snippet will generate a *compilation error* because the *division* and *multiplication* operators are *not supported* by *character types*. char mychar1 = 'a'; char mychar2 = 'b'; System.out.println(mychar1 * mychar2); System.out.println(mychar1 / mychar2); True/False?

False

The below loop code is valid and will not generate a compilation or run-time error. for(int i = 0; i <= 10; i++) { int sum = 0; sum = sum + i; } System.out.println(sum); True/False?

False

When creating a File object with an invalid path String parameter, Java will throw an error immediately when instantiating the Object. True/False?

False

What will the below code snippet produce when attempting to run? String input_value = "6"; int upper_bound = 10; if(input_value < upper_bound) { System.out.println("input_value is less than upper_bound"); } else { System.out.println("input_value is greater than or equal to upper_bound"); } A. "input_value is less than upper_bound" B. "input_value is greater than or equal to upper_bound" C. It will give a compilation error due to a type mismatch for input_value and upper_bound variable comparison D. No output will be printed

It will give a compilation error due to a type mismatch for input_value and upper_bound variable comparison

Parsing

Java provides class objects that allow you to parse strings into native primitive data types such as int, float, double, etc.

Methods are removed from the *call stack* in the order of:

Last in First Out

Boolean operators symbol: ( *<*)

Less than

Boolean operators symbol: ( *<=* )

Less than or equal to

A __________ is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in this data structure are linked using pointers as shown in the below image:

LinkedList

Provides little to no abstraction from a computer's instruction set architecture Code runs directly on the processor Relatively non-portable, program written are targeted to a specific computer architecture (PowerPC, x86, x64, ARM, etc.)

Low-level language

When labeling an attribute or method with the *public* or *private* keyword you are using what's called a....

Modifier

Boolean operators symbol: ( *!* )

NOT

Is String a primitive data type?

No, it is an non-primitive type (its an object)

Boolean operators symbol: ( *!=* )

Not Equal to

Decisions

Check for certain conditions and execute the appropriate code.

Boolean operators symbol: ( *|* )

OR

Float and Decimal

Representing numbers with a fractional component can be complicated Components that make the format are typically the *sign* bit, *exponent*, and the mantissa

A ___ is a linear data structure which follows a particular order in which the operations are performed.

Stack

Variable Assignment

a = 3;

&, ^, |

bitwise opeartors that apply a bit mask

The ____ is a mechanism used to store method calls to resume execution when another method returns.

call stack

Strings can be assigned as a value by using

double quotes

__________ when instantiating the Scanner Object will allow you to use the same iterative methods you use to grab keyboard input, to grab file data as well.

passing it a File Object

Calculations

perform some sort of mathematical calculation, algorithm, or task to process data from one form to another

>> and <<

shift bit (left or right)

Character can be assigned as a value by using

single quotes

Character (Definition)

single symbol that represents a letter, number, symbol typically found a keyboard or a character set

Operators

symbol that represent simple computations

Boolean operators symbol: ( *&* )

AND

Boolean operators symbol: ( *||* )

the short-circuit OR operator

In a main method definition, the word void is called:

to specify that the main method doesn't return anything

Logical operators consists of

&&, ||, !=

Creating a Method

A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). access type return type name(){ //somecode } Ex: public class MyClass { static void myMethod() { // code to be executed } }

A constructor is...

A special method that initializes and builds a newly constructed object

Abstract Class

a class that defines attributes and methods for subclasses but is never instantiated

Abstract class

a class that is declared abstract, meaning it cannot be instantiated using the new keyword; it can be sub-classed; sub-class objects may be converted to an abstract type after instantiation

An ________ is a method that is declared without an implementation (no curly braces, ending with a semi-colon)

abstract method

A loop that is nested within another loop (inner loop) has access to

all of the variables that were declared in the first loop (outer loop)

The Java compiler generates __________.

byte code

What are the primitive data types?

byte, short, int, long, float, double, boolean, char

A String is a

collection of characters

Declaring Variables

ex: String message; ex: int a; (I'm going to use a variable named "a" to store an integer value.)

Java, Python, C and C++, Ruby, JavaScript

high level languages

Strong abstraction from details of the computer workings. Takes less time to write, shorter and easier to read, and are more likely to be correct. Relatively portable, program source code can be run against an interpreter or go through a compilation step to target specific architectures.

high-level language

A degree of rounding error is introduced

into every operation performed on floating point numbers

main method definition

is the entry point into the application

Microsoft Assembler (MASM), GNU Assembler (GAS), Netwide Assembler (NASM)

low-level language

You must have a __________ in order for your Java program to run, otherwise you will receive an error.

main method

%

modulus (remainder)

Variable

named location that stores a value

When creating a reference type such as an array, what keyword must be used to create the reference type in memory? See below code examples and fill in the blank int counts[] = __ int [4]; int counts[] = ___ int [] {1,2,3,4};

new

Values that the operators work with

operands

How would you declare a new class called *FastFoodJoint* that inherits all of the attributes and functions from the *Restaurant* class?

public class FastFoodJoint extends Restaurant {...}

Main method is written as:

public static void main(String[] args)

Ternary Statement Syntax

result = textCondition ? trueValue : falseValue (Examples // result is assigned the value "Sorry Dude, it's false" String result=false? "Dude, that was true" : "Sorry Dude, it's false"; )

Boolean operators symbol: ( *&&* )

the short-circuit AND operator

Overflow (definition)

try to add the two integers together and the result is larger than what the data type (condition where you cross the limit of prescribed size for a data type)

For the below program, what will the array variables *names* contain at the end of the program? public class TestQuestion { public static void main(String[] args) { String[] names = new String[] {"John", "Josh", "Jacob"}; String[] names_fuzz = addFuzz(names); } public static String[] addFuzz(String[] arg_array) { for(int i = 0; i < arg_array.length; i++) { arg_array[i] = arg_array[i] + "Fizz"; } return arg_array; } } A. null B. {"John" , "Josh" , "Jacob" } C. {} D. {"JohnFizz" , "JoshFizz" , "JacobFizz" }

{"JohnFizz" , "JoshFizz" , "JacobFizz" }

Looking at the below code snippet, what is the resulting elements for the string array variable fruits? String[] fruits = new String[] {"apples", "oranges", "bananas"}; String[] fruitsCopy = fruits; fruitsCopy[1] = "grapes"; A. {"apples", "oranges", "bananas"} B. {"grapes", "oranges", "bananas"} C. {"bananas", "oranges", "apples"} D. {"apples", "grapes", "bananas"}

{"apples", "grapes", "bananas"}

Output

Display data on screen, send data to a file or other device

In what order are items removed from a stack interface?

LIFO - Last in First Out

True && True =

True

What is the output produced by the below code snippet? int a = 5; float b = 1.5f; int result = (int) (a * b); System.out.println(result); A. 6 B. 7.5 C. 7 D. 7.0000000

7

What will the below loop code output to the console? for(int i = 10; i > 0; i--) { if(i % 2 > 0) { System.out.print(i + " "); } } A. 2 4 6 8 10 B. 1 3 5 7 9 C. 9 7 5 3 1 D. 10 8 6 4 2

9 7 5 3 1

*Logical operators* &&, ||, ! are known as:

And, Or, Not

A __________ in a code editor allows you to stop execution of your program during a debugging session in order to inspect variable values and step through your source code one line at a time. A. Breakpoint B. Print Statement C. Conditional Statement D. While Loop

Breakpoint

Of the programming languages listed below, which one is not an example of a High-level programming language? A. Java B. GNU Assembler (GAS) C. Python D. Ruby E. None of the above

GNU Assembler (GAS)

Input

Get data from the keyboard, a file, a sensor, or some other device

Boolean operators symbol: ( *>* )

Greater than

Boolean operators symbol: ( *>=* )

Greater than or equal to

A programming language that is designed to be easy for humans to read and write due to the language's strong abstraction of the intricate details involved with a computer's inner workings.

High-level programming language

Java strings are ________, meaning that once the string is created the character data cannot be edited in anyway. A. Mutable B. Immutable C. Executable D. None of the above

Immutable

for-each loop

Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed by the array name. ex: for (String name : names) { System.out.println(name); }

IDE stands for

Integrated Developer Environment

Having more than one method with the same name, but different parameters, is called:

Overloading the method

A Do - While loop, like the snippet below, is useful because it allows the programmer to: int i = 5; do { System.out.println(i); i++; } while (i <= 10); A. Perform a pre-test of a condition so that you can check for a condition before executing loop code B. Perform a post-test of a condition so that you can run the loop code at least once before performing a loop test C. All of the above D. None of the above

Perform a post-test of a condition so that you can run the loop code at least once before performing a loop test

Repetition

Perform some action repeatedly possibly with some variation over a set of instances

You can use the ___ Object to not only read keyboard input, but read the contents of a file as well by passing it a File Object.

Scanner

The *static* keyword indicates that the attribute or method is...

Shared or usable across all instances of the class

If you have a method that contains code that calls another method that can throw an exception you have to do either one of two things... NOTE: Multiple answers are available for this question, select two answers below A. Specify that your method will throw an exception by using the "throws" keyword in the method definition B. Put an If/Else statement to not allow your code to hit the method at all C. Surround the invoked method with a try/catch statement in order to handle the exception that may occur D. Return from the method before calling the method that causes an error

Specify that your method will throw an exception by using the "throws" keyword in the method definition Surround the invoked method with a try/catch statement in order to handle the exception that may occur

When calling the flush method when using a FileWriter Object, you are telling the program to A. Take the current data held in the FileWriter's buffer and write the contents to the file immediately B. Reset your file pointer and start over C. Force the garbage collector to remove any corrupted data in your program D. Dispose of the FileWriter Object

Take the current data held in the FileWriter's buffer and write the contents to the file immediately

IDE

Text editing programs with advanced features to allow for rapid development of code. Features usually include: •Syntax Highlighting •Code Completion & Snippets •Automatic Code Indentation & Folding •Execution of code within editor •Advanced Debugging Tools

What will be the produced output when running the below code snippet? String first_name = "Adam"; String last_name = null; System.out.println(first_name.toUpperCase() + " " + last_name.toUpperCase()); A. ADAM B. The code snippet will produce a run-time error when executing the last print statement line C. ADAM NULL D. Adam null

The code snippet will produce a run-time error when executing the last print statement line

Overflow can be detected if and only if

Two numbers with the same sign are added and result in a number with opposite sign Ex: 5+(-3) = 2

A *recursive method* is a method that invokes itself within it's code body, using a *base case* to determine when to stop the successive calls to itself. True/False

True

A class may only inherit from one super class, but can implement many interfaces. True/False

True

A degree of rounding error is introduced into every operation performed on floating point numbers. True/False?

True

Java programs can be considered both a compiled and interpreted. Meaning, instead of translating programs directly into machine language, the Java compiler generates byte code. True/False?

True

Valid Java code must honor the "Catch or Specify" requirement. Code that might throw certain exceptions must be enclosed by either of the following: - A try statement that catches the exception. The try must provide a handler for the exception - A method that specifies that it can throw the exception. The method must provide a throws clause that lists the exception True/False

True


Set pelajaran terkait

FINC314 Exam 1 Practice Questions

View Set

nutrition exam(sections 2 and 3)

View Set

Chapter 12 - TERMS AND QUESTIONS*

View Set

Marquis de Lafayette Information

View Set

NUR 4770-Exam 2: Renal Practice Questions

View Set

Nursing Fundamentals NCLEX Questions

View Set

Chapter 3 - Storage devices and media

View Set

9.1 & 9.3 Origins of Progressiveness & Teddy Roosevelt's Square Deal

View Set