1/205
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Name 8 primitive types in Java
int, double, short, long, float, byte, boolean, char
Strings are...
immutable, Objects, Not Primitive Types, Made up of letters
default int
0
default char
\u0000
default double
0.0d
default byte
0
default float
0.0f
default long
0L
default boolean
false
default short
0
Which of the following is the data type used for holding a single character?
a) Strings
b) char
c) int
d) all of the above
d) all of the above
what does immutable mean in terms of Strings?
Once you set a String to a specific value, you cannot change it character by character
Assume you have this line of code in your project:
String eclipse = "Not IntelliJ!";
What would invocation of eclipse.length() return?
13
How to write switch statement?
switch(str) {
case "CS180":
//print something
break;
default;
//print something
break;
}
-> switch statements
switch(number) {
case 0 -> System.out.println("zero");
case 1 -> System.out.println("one");
default -> {
case 11, 13, 15 -> System.out.println("An odd number");
case 12, 14, 16 -> System.out.println("An even number");
}
}
when is && used?
when both or all conditions must be evaluated as true in order for the conditional to evaluate true
when is || used?
when one or both conditions must be evaluated as true in order for the conditional to evaluate true
What can boolean evaluation in if statements use?
logical operations (AND &&, OR ||) or Bitwise (AND &, OR ^, OR |)
What does bitwise return?
1 - if results are different
0 - otherwise
What will the following block of code print?
for(int i = 0; i < 3; i++) {
switch(i) {
case 1:
System.out.println("1");
case 2:
System.out.println("2");
case 3:
System.out.println("3");
}
}
12323
What will the following block of code do?
if (0b10 != 0b1)
System.out.println("false");
false
What are the four different types of loops?
while, do-while, for, Enhanced for loop (also called for-each loop)
Does while loop execute at least once or upon entry of the loop?
upon entry of the loop
Does do-while loop execute at least once or upon entry of the loop?
At least once, this is due to the condition being checked at the end of the loop, rather than the beginning like a while loop
Does for loop execute at least once or upon entry of the loop?
execute at least once except if the conditions in the for(int i = 0...) is not executable
what is an enhanced for loop?
The enhanced for loop can be used to iterate over a set of data, like a list or an array. It can also be used for any class that implements the Iterable interface
public void enhancedForLoop() {
int[] arrayOfNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i : arrayOfNumbers) {
System.out.println(i);
}
}
Which of the following is not a loop in Java?
a) for-each
b) for-every
c) for
d) while
b) for-every
Which of the following is the loop where at least one execution is guaranteed?
a) for-each
b) for
c) while
d) do-while
d) do-while
Which of the following loops is best for handling large data sets with an unknown size?
a) for-each
b) for
c) while
d) do-while
a) for-each
for (int i : array){}
What is in a methods signature?
Their name, their number, order and types of their parameters
define static
static, for making methods accessible without object declaration, In other words, they are class level methods.
define final
final, for making methods or fields, non modifiable. The values there are considered final, and once initiated, cannot be modified.
define transient
transient, if an object is going to be serialized, fields marked with the transient modifier will be excluded when the object is written. In other words, it indicates a field that should not be persistent between serialization.
define serialization
allows us to convert an Object to stream that we can send over the network or save it as a file or store in a database for later usage
what does using final in a field mean?
if a field is final, it cannot be modified in a method, and must be initialized at the time of declaration
You can also assign final fields inside a constructor, barring the fact that their value cannot be changed at a later time in the program
what does using static in a field mean?
using static with fields means that the field is at a class level an can be accessed without creation of an object
Should methods and fields be public or private by convention?
fields should be private and methods should be public
write a setter method or mutator method
public void setAnInt(int value) {
anInt = value;
}
write a getter method or accessor method
public int getAnInt() {
return anInt;
}
Which of the following is considered to be part of the method signature?
a) throws declaration
b) access modifier
c) final modifier
d) method name
d) method name
Which of the following is the modifier to determine an unchangeable, final, value?
a) default
b) public
c) final
d) static
c) final
What is the default access modifier for a method?
a) public
b) private
c) static
d) package-private
d) package-private
Getter and Setter methods are also called ______ and ______.
a) access modifier and deaccess modifier
b) grabbers and placers
c) accessor and mutator
d) mutator and accessor
c) accessor and mutator
what are three ways to initialize an array?
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = new int[20];
int[] array3 = new int[]{1, 2, 3, 4, 5};
What is the last position in the following array?
int[] array2 = new int[20];
19
what is an example of a 2D array?
String[][] array = new String[10][10];
what is used to loop through a 2D array?
matrix.length and matrix[0].length
what is a ragged or jagged array?
When the columns are not the same amount for each row
int[][] raggedArray = new int[3][];
raggedArray[0] = new int[5];
raggedArray[1] = new int [3]
raggedArray[2] = new int[4];
what are two ways to initialize an arrayList?
ArrayList<type> someList = new ArrayList<type>();
Or you can have Java infer it:
ArrayList<type> someList = new ArrayList<>();
What are the two most common methods to use for ArrayLists?
set(): changing a value at a specific index
add(): add an element in between specific indicies, implicitly shifting all elements right by one.
What does the <> brackets in a definition of an ArrayList mean?
a) Inferred data types from the initialization
b) Part of the ArrayList class
c) Defines something as a list
d) Generic brackets defining data type
d) Generic brackets defining data type
True or False - Arrays can only be a maximum of 3 dimensions.
False
True or False? Arrays have set sizes.
True
What is a pattern that should be followed when using FileIO?
Java -----> Open -> Read
\
> Write
A file needs to exist in order to read it (FileIO)
True
A file needs to exist in order to write in it (FileIO)
false
the file doesn't have to exist, and once the data is flushed to the file, the files will be created.
What are the three levels of FIle I/O?
Low Level
High Level
Object Level
What is a Low Level File I/O?
- Raw data - byte oriented
- Mainly used FileOutputStream and FileInputStream to process data in and out of the file.
What is a High Level File I/O?
- Primitive types, for File I/O such as int, long, short, double, char and even String
- Normally use DataOutputStream and DataInputStream
What is a Object I/O (Serialization)?
- Objects that you create or objects from a Java package
- Done using ObjectOutputStream and ObjectInputStream
- If it's done using your own class, you must include the implements Serializable keyword in the class header
Why would an OptionalDataException be thrown?
If the data was not received in the same order that it was sent
Which reader type is best for high volumes of data?
BufferedReader
What do you have to do to ensure that data is written to a file every time?
a) Make sure the file exists
b) Ensure that you flush after every write statement
c) Verify that the file is empty before writing to it
b) Ensure that you flush after every write statement
When reading from a file what do you have to do before reading?
a) Make sure the file exists
b) Attempt to read data from the file before writing
c) Ensure that you flush every write statement
d) Verify that the file is empty before writing to it
a) Make sure the file exists
When performing an append operation on a file during the writing process, which are valid ways of doing this?
a) Read all the data into an ArrayList, and append the data into the list, which you would then overwrite the file with.
b) Write to the file using the append operation of a writer that supports this.
c) Write to the file, and hope the writer doesn't delete past data.
d) Do a check on whether the file already has data in it - if it does, read that in, and then write new data to the file, including the old data.
a) Read all the data into an ArrayList, and append the data into the list, which you would then overwrite the file with.
b) Write to the file using the append operation of a writer that supports this.
d) Do a check on whether the file already has data in it - if it does, read that in, and then write new data to the file, including the old data.
What is the name of the exception that is thrown is the file cannot be found, when writing data?
a) ArrayIndexOutOfBoundsException
b) FileNotFoundException
c) OptionalDataException
d) No exceptions are thrown
d) No exceptions are thrown
What is the name of the exception that is thrown if the file does not exist, when reading data from the file?
a) ArrayIndexOutOfBoundsException
b) FileNotFoundException
c) OptionalDataException
d) No exceptions are thrown
b) FileNotFoundException
What does the class extend when writing a custom exception?
extends Exception
What is the name of errors that are reported in the code?
stack trace
What is an example of a try catch?
try {
//code
} catch (Exception e) {
//do something
}
what does final do in a try catch block?
the code in final clause will run everytime
example of a try-with-resources block
try (PrintWriter writer = new PrintWriter(new File("Test.txt"))) {
writer.println("Hello World")
}
Resources are allocated inside the parenthesis of the try block, and are automatically close at the ends of the try block or if the catch blocks executes
Which of these exception(s) is unchecked almost always?
a) NullPointerException
b) IOException
c) StackOverflowException
d) All of the above are checked exceptions
a) NullPointerException
How can you ensure that resources are shut off before program ends?
a) Use the reader's close() method, or the equivalent.
b) Use a try-wtih-resources statement
c) Hope that your program closes all resources
d) A and B
d) A and B
When using a try-with-resources statement, what block is implied?
a) Catch Block
b) Finally Block
c) No code blocks are implied
d) Try Block
b) Finally Block
When using a standard try-catch clause, what is guaranteed to run after every try-catch statement?
a) Try Block
b) The throws statement
c) Catch Block
d) Finally Block
d) Finally Block
(Should be known for the Exam) What does implements Serializable do?
Marks a class as something that will be transferred over a stream, such as a FileOutputStream or a Socket connection.
(Should be known for the Exam) What does implements Iterable do?
Defines a class that can be iterated over, in a loop for example
What does having a default method in an interface mean?
This defines the method to contain code that does not have to be implemented every time.
public interface anInterface {
default public void displayNameDefault(String name) {
System.out.println("Your name is " + name);
}
public void displayName(String name);
public void displayNameAndJobTitle(String name, String jobTitle);
}
Can you extend more than one super class?
No
Can you implement multiple interfaces?
Yes
If you extend a class what must you put in the sub classes constructor?
The first line must be an explicit call to the super class's constructor, via the super() call
How do you use instanceof in terms of inheritance?
public class Employee extends Person{}
By using the instanceof keyword here , you would be able to tell if an object of the Person class is an instance of an Employee
What is the difference between Overloading and Overriding?
Overloading is when you have two methods of the same name, but different signature - that is, different(number and types of) parameters and return type.
Overriding is when you have a method in the super class that you are writing in a child class to perform something different than the super class. Think about equals() or toString() methods. For overriding, the signature and name must be identical.
Which of these interfaces require implementation of 0 methods?
a) Serializable
b) Iterator
c) Comparable
d) Runnable
a) Serializable
Suppose you have two constructors, one that takes an integer, and one that takes an integer and a String. How can you call the two parameter constructor from the one parameter constructor? Assume the integer is called num, and the String, str.
a) this(num, str)
b) super(str, num)
c) this(str, num)
d) super.this(str, num)
a) this(num, str)
Can you have two methods with the same name in the same class?
a) No, Java doesn't allow this and your code will not compile.
b) Yes, provided that each methods signatures are different
c) No, because Java will not know which method to go to upon call.
d) Yes, because Java is smart enough to decide which one to choose based on the program's execution
b) Yes, provided that each methods signatures are different
How do you define an abstract class?
By declaring all the abstract methods
abstract String getDetails();
abstract int getAmount();
What is happening in this code?
public void makeItRain(ArrayList
ArrayList
for(Money m : bills) {
m.tossInAirRecklessly();
}
}
Java will invoke the tossInAirRecklessly() method for each Money object, invoking the specific implementation of whichever class the Money object was instantiated to
When writing a method with the same name and same signature as that of the super class in a child class, what is this concept called?
a) Overloading
b) Method overloading
c) Method Overriding
d) This concept is not allowed in Java
c) Method Overriding
When working with super classes, what is the modifier used to indicate that a method must be written in a child class?
a) static
b) abstract
c) implements
d) super.methodNameHere();
b) abstract
What is the concept called for Java deciding which method to use when incorporating a list of an abstract class?
a) Static Binding
b) Dynamic Binding
c) Dynamic Decision Making
d) Static Decision Making
b) Dynamic Binding
When running a program normally what is this called?
You are running the program sequentially
What is it called when you are running the program with threads?
You are running the program concurrently
What are two ways of creating threads in java?
Implementing the Runnable() interface and through that, implementing the run() method
or extending the Thread class
What are the four most useful methods to you in thread management for a Thread object called t?
t.start(), which starts the Thread and invoke the run() method
t.join(), which waits for another Thread to finish before executing
t.yield(), which pauses the execution of the Thread
t.sleep(....), which pauses the Thread for a parameterized amount of time in milliseconds
When do Race conditions occur?
When shared data is accessed by different threads simultaneously
Another way of putting it is two threads racing to update a field or get the newest value of a field
How to avoid race conditions?
shared data should have synchronized keyword. Think of it like a gatekeeper, in where only one thread is allowed to modify the field or get the newest data at a time.
Which of these methods do you use for making a Thread wait until another one finishes? Assume your thread object is called t.
a) t.start()
b) t.join()
c) t.yield()
d)t.run()
b) t.join()
Which of these methods do you use for making a thread wait for a specified amount of time before resuming?
a) t.join()
b) t.sleep(....)
c) t.yield()
d) t.interrupt()
b) t.sleep(....)