1/107
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Is it possible to create an array in Java where the first row has 5 elements, the second row has 6 elements and the third row has 2 elements?
yes
create a for loop to execute: Is it possible to create an array in Java where the first row has 5 elements, the second row has 6 elements and the third row has 2 elements?
double [] [] salaries = new double [NUM_STORES][];
array[0] = new double [5];
array[1] = new double [6];
array[2] = new double [2];
Write the statement to print the number of elements in a one-dimensional array.
for(i =0; i
What does Java initialize the elements of an array of references to?
null
What does Java initialize an array of ints to?
0
Is an array an object?
Yes
int [ ] numbers = new int[16] how many objects were created?
1, new was used once
Student [ ] roster = new Student[25] <- how many objects were created?
1, new was used once
Write the command to compile a source code file named Book.java
javac Book.java
Write the command to run a class file named Book.class
java Book
Where does the execution of a Java project begin?
public static void main(String [] args)
How many public classes can a Java class file contain?
1
Can you declare fields of a class inside a method?
No
What does private access for a field mean?
Only the current class will have access to the field or method.
What does public access for a field mean?
Any class can refer to the field or call the method.
What does protected access for a field mean?
Only the current class and sub classes (and sometimes also same-package classes) of this class will have access to the field or method.
What happens if you do not specify the access for a field?
It will set the access to package
Can a sub-class also be a super-class?
Yes, Java allows this
What is an abstract method?
It is a method that is declared, but contains no implementation
What is an abstract class?
Is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub classed.
Does a super class have to be abstract?
No
How many classes can a class directly inherit from?
1
How many interfaces can a class implement?
Multiple or unlimited
What can a Java interface contain?
Consists of modifiers, the keyword interface, the interface name, a comma-separated list of parent interfaces (if any), and the interface body
Can an interface be instantiated?
You can never instantiate an interface in java.
Does the is-a relationship apply to classes that implement an interface?
No
What is the difference between overloading a method and overriding a method?
Overloading: is the ability to create multiple methods of the same name with different implementations.
Overriding: is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.
What class does every class indirectly inherit from?
Object class
What is a method every class has whether you write it or not?
object.equals() method
What causes a null-pointer exception?
is thrown when an application attempts to use an object reference, having the null value.
What is the purpose of the dot operator?
It allows one to invoke a method within an object when given a reference to the object
What is meant by encapsulation?
is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as as single unit.
What type of Exceptions require the programmer to acknowledge?
Checked Exceptions
What type of Exceptions do not require the programmer to write any additional code?
Unchecked or Run time Exceptions
Name three checked exceptions
SQLException
IOException
DataAccessException
ClassNotFoundException
InvocationTargetException
Name three RuntimeExceptions.
Also called unchecked examples,
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
IllegalArgumentException
What pre-written class is used to write data to a file?
Print Writer
What pre-written class is used to break a String into smaller Strings?
String Tokenizer
Write the code for opening a file named: accounts.txt for input.
String fileName = "C://clientdata.txt";
input = new Scanner(new File(fileName));
Use the following hierarchy to answer 40-43
public abstract class BankAccount
public class SavingsAccount extends BankAccount public class CheckingAccount extends BankAccount public class CheckingPlus extends CheckingAccount
Which of the following are valid declarations?
a) BankAccount bA = new BankAccount( ):
b) BankAccount bA = new SavingsAccount( );
c) BankAccount bA = new CheckingPlus( );
d) CheckingAccount bA = new CheckingPlus( );
e) SavingsAccount bA = new CheckingAccount( );
f) CheckingAccount bA = new CheckingPlus( );
g) ChekingPlus bA = new CheckingAccount( );
B, C, D, AND F
If BankAccount( ) class has an abstract method named withDrawFunds ( ) what do you know about the SavingAccount class and the method withDrawFunds( )?
That the savings account class will have a method called withdraw funds
If BankingAccount( ) has a protected method named depositFunds( ) can the CheckingAccount class override this method with a private method named depositFunds( )?
No because it has to be the same or less restrictive
What will happen if the BankAccount class does NOT have a default constructor written, the CheckingAccount does have a default constructor and main( ) tries to create an instance like this: CheckingAccount cA = new ChekingAccount( );
All fields in the default CheckingAccount will be set to null.
Is the call to the super's constructor always made?
Yes
If a subclass explicitly calls the constructor of the superclass, where must the call be made in the subclass constructor?
It will call the default if not specified
Can a subclass write methods that do not appear in the superclass?
Yes
Must a subclass override every method of its superclass?
No, it just has to override all of the abstract methods
T/F The finally block of a try/catch/finally only executes if the method throws an Exception
False, it always executes
What is meant by an Exception propagating up the call stack
An exception propagates from method to method, up the call stack, until it's caught. So if a() calls b(), which calls c(), which calls d(), and if d() throws an exception, the exception will propagate from d to c to b to a, unless one of these methods catches the exception.
When using the BlueJ debugging tools, what is the difference between the Step command and the StepInto command?
Step into will jump from class definition file to class definition file. Step stays in the class definition file.
What is a Java package?
is a namespace that organizes a set of related classes and interfaces
What is contained in a Java package?
Classes and Interfaces
Does the Java compiler compile source code to machine code?
No, It compiles source code to byte code
Why does a person who wants to run a Java program need a Java Virtual Machine installed on their computer?
Because java code is not a compiled language so it needs something to run it.
What is the purpose of the import statement?
in Java, it allows to refer to classes which are declared in other packages to be accessed without referring to the full package name.
Does using the import statement make your compiled class file larger?
No
T/F If a method throws an Exception and the Exception is caught in a catch block, the method continues executing the code after the catch block.
True
Write the class definition for a check-exception named: ImproperPriceException:
public class Myexception extends IOException{
Write the class definition for a RuntimeException named: ImproperPriceException:
public class My exception extends RuntimeException{
What are the 4 components (concepts) every object-oriented language must have?
Encapsulation, Data Abstraction,
Polymorphism and Inheritence
What is the return type of the compareTo( ) method?
integer 0,1
What is the parameter to the equals( ) method?
objects being compared
What are the parts of a method signature?
Name of the method and parameters
Can two methods in the same class definition file have the same signature?
No
What must a class do to implement an interface besides declaring that in the class heading?
Overide all methods
What method is used to retrieve the error message from an Exception
getMessage();
What is the access specifier for all methods in every Java interface?
public
In the following class heading, which is the subclass?
public class ClassA extends ClassB implements ClassC
ClassA
Any object that can be thown as an Exception must inherit from what class?
Throwable
What is the correct header for a method that will accept an array of ten integers as an argument?
public void printMe(int [10] in array))
If price has a value of 25, what is the value of discount after this statement? discount = price < 25? .10: .20;
.2
What does the term: polymorphism actually mean in English?
the condition of occurring in several different forms.
T/F: All static fields must be declared as final?
False
T/F: A constant must be given a value in the same statement in which it is declared.
True
T/F: The programmer must write a constructor method for each class he/she writes or the class will not compile.
False
Look at this method heading:
Date addDays(int innumber) What is the return type from this method?
object reference variable of a date object
Is the following an abstract method?
int addValue(float amount) { }
No
What is the name of the Java compiler?
Javac
What are the two categories of data types in Java?
primitive and reference
What numeric data type holds an exact value?
int
Can a field declared as type boolean hold a value of 1?
No
What primitive Java data type can hold the largest integer value?
long
What are the four levels of access for fields and methods in a class definition file?
public, protected, default, private
Which level of access gives access to other classes that are sub-classes?
protected
Which level of access gives access to any other class?
public
Which level of access restricts access to only methods in the same class file?
private
What is the keyword used to give access to other classes in the same package?
package
Why is public access a dangerous access that should be rarely used?
It breaks encapsulation and anyone can change values of fields
What is an interface comprised of in Java 7?
method heading
Can an interface have fields?
Yes
What is the access of all the methods in a Java interface?
public
Can an interface be instantiated?
No
Give two reasons for interfaces in the Java language.
To implement a list of methods and organization
How many interfaces can another class implement
As many as needed
What does it mean to implement a Java interface
Create a class that overrides all the methods
If a class extends an Employee class and implements a Comparable interface, what are the is-a relationships established?
The given class is a: is - a relationship
How many classes can a given class extend?
one
Name five Java pre-written interfaces
N/A
What are the two broad categories of Exceptions in Java?
Checked and Unchecked(Run Time)
What is the major difference between a checked exception and an unchecked exception?
Checked: is all those Exception which requires being catches and handled during compile time.
Unchecked: is those Exceptions whose handling is not verified during Compile time.