OOP Midterm
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);
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 + " "); } }
9 7 5 3 1
A High-level programming language can be defined as...
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.
A constructor is... A class that uses objects in another class A special method that initializes and builds a newly constructed object A static method that depends only on it's parameters and no other data A method that returns the value of an instance variable
A special method that initializes and builds a newly constructed object
T/F 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(mychar * mychar2);System.out.println(mychar / mychar2);
False
T/F An abstract class can be instantiated using the new keyword.
False
T/F 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);
False
T/F When creating a File object with an invalid path String parameter, Java will throw an error immediately when instantiating the Object.
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"); }
It will give a compilation error due to a type mismatch for input_value and upper_bound variable comparison
The call stack is a mechanism used to store method calls to resume execution when another method returns. Methods are removed from the call stack in the order of: First in First Out Last in First Out Random Order None of the above
Last in First Out
When labeling an attribute or method with the public or private keyword you are using what's called a....
Modifier
Having more than one method with the same name, but different parameters, is called:
Overloading the method
The static keyword indicates that the attribute or method is...
Shared or usable across all instances of the class
When calling the flush method when using a FileWriter Object, you are telling the program to Take the current data held in the FileWriter's buffer and write the contents to the file immediately Reset your file pointer and start over Force the garbage collector to remove any corrupted data in your program Dispose of the FileWriter Object
Take the current data held in the FileWriter's buffer and write the contents to the file immediately
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());
The code snippet will produce a run-time error when executing the last print statement line
T/F A degree of rounding error is introduced into every operation performed on floating point numbers.
True
T/F 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
T/F 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
T/F 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
T/F You can use the Scanner Object to not only read keyboard input, but read the contents of a file as well by passing it a File Object.
True
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
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; } }
{"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";
{"apples", "grapes", "bananas"}
Overriding can be defined as... Wrapping a primitive type around a class Allowing a subclass class to overwrite and provide a specific implementation of a method that is already defined by one of its super-classes Converting from one object type to another
Allowing a subclass class to overwrite and provide a specific implementation of a method that is already defined by one of its super-classes
Logical operators &&, ||, ! are known as:
And, Or, Not
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
B. GNU Assembler (GAS)
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
B. Perform a post-test of a condition so that you can run the loop code at least once before performing a loop test
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.
Breakpoint
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
C. Compilation error would occur. Cannot access the age attribute outside of the Person class
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); } } number is equal to twenty number is equal to 0 Could not parse string_number number is equal to 20
Could not parse string_number
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 Specify that your method will throw an exception by using the "throws" keyword in the method definition Put an If/Else statement to not allow your code to hit the method at all Surround the invoked method with a try/catch statement in order to handle the exception that may occur 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
Java strings are ________, meaning that once the string is created the character data cannot be edited in anyway.
immutable
