Documente Academic
Documente Profesional
Documente Cultură
Write a single line of code that instantiates a file Scanner to read from a file
called numbers.dat. Scanner fileScanner = new Scanner(new
FileInputStream(numbers.dat));
2. Which methods do we use to read values from a
file using a Scanner object? nextLine(), nextInt(), nextDouble(), nextBoolean(), etc...
3. Write a single line of code that instantiates a file PrintWriter to write to a file called
names.txt. PrintWriter fileWriter = new PrintWriter(new
FileOutputStream(names.txt));
4. Which methods do we use to write values to a file
using a PrintWriter object? print() and println()
5. What Exception can be thrown when
instantiating a Scanner or PrintWriter for file IO? Is this exception checked or unchecked?
FileNotFoundException, checked
6. Write a segment of code that prints the contents
of a file to the Java console. Assume you have a file Scanner called myFileScanner. while
(myFileScanner.hasNextLine()) { System.out.println(myFileScanner.nextLine()); }
7. Write a try-catch-finally block that correctly opens a PrintWriter object and closes it
after use. Your PrintWriter should write to a file called output.txt. PrintWriter writer =
null; ....try { writer = new PrintWriter(new FileOutputStream(output.txt)); }
catch (FileNotFoundException ex) { System.out.println(ex.getMessage()); } finally
{ if (writer != null) { writer.close(); } }
8. True or false The File class can be used to
represent both files and directories. true
Recursion 1. What are the 3 rules of recursion? Include a base case, Approach your
base case, Make your recursive call , TRUST THAT IT WORKS! 2. What is the Java call
stack and why is it important? The java call stack manages the variables and return
types associated with an method calls. It is important to understand how methods
are added or removed from the stack during a recursive method call.
3. Suppose you had the following recursive method. public void foo(int n) { //base case if (n
== 30) { return; } else { //print n System.out.println(n); //recurse foo(n + 5); } } What is
wrong with the following code segment? What do we call this behavior? foo(21); This will
skip over the base case. This is infinite recursion!
.Abstract Classes 1. For the following Fruit class, add an abstract method called
isHealthy() with a return type of boolean. public abstract class Fruit { public abstract
boolean isHealthy(); }
2. Suppose your are given the following class. public abstract
class Ship { private int maxCapacity; public Ship(int myMaxCapacity) { maxCapacity =
myMaxCapacity; } } What is wrong with the following code segment? Ship titanic = new
Ship(1000); You cannot instantiate an abstract class. 3. Circle any of the following that
are true concerning abstract classes. a. Abstract classes cannot have constructors b. You
cannot instantiate an abstract class c. An abstract class can only include one abstract
method d. Abstract classes cannot contain fields e. A child class of an abstract class
must implement all abstract methods, or it must also be marked abstract 4. True or false A
class can choose to not implement an abstract method that has been inherited. true
.Console Programming 2. What types of values can you send as an argument to
System.out.print() and System.out.println()? most of the primitive types, and object
types
3. Can you pass object (reference) types to the System.out.print() and
System.out.println() methods? What output is sent to the console? Yes, it will print out the
toString() method of the object.
4. Write a single line of code that creates a Scanner
object to read from the Java console. Scanner myScanner = new Scanner(System.in);
5. What method do we call on a Scanner object to read a String from the Java console?
nextLine() 6. Write a loop that prompts the user for three numbers and prints them to the
Java console. Assume you have access to a Scanner object called myScanner. for (int i =
1; i <= 3; i++) { System.out.println(Please enter a number: ); int number =
myScanner.nextInt(); System.out.println(The number you entered was: +
number); }
.The Random Class 1. What is wrong with the following code segment? Random rand =
new Random(); //prints a random number between 1 and 90 .
System.out.println(rand.nextInt(90)); The comment is incorrect. This will print a number
between 0 and 89. 2. What does it mean that the random class generates pseudo-random
numbers. Computers have difficult generating truly random numbers. The numbers
generated by the Random class are pulled from a series of numbers generated by a
mathematical algorithm. The only random part about the routine is where to begin
retrieving numbers from the series. 3. Write a segment of code that prints random
numbers between 5 and 15. Assume you have access to a Random object
calledmyRandom.System.out.println(myRandom.nextInt(11) + 5);
String Manipulation 1. What does it mean when we say that the String class is immutable?
Be as clear as possible when answering this question. A string object cannot be changed
once it has been created.
Exceptions1. Write a single line of code that throws an IllegalStateException with the
following message: Stop that! Stop that right now! throw new
IllegalStateException(Stop that! Stop that right now!); 2. Write a try-catch block that
surrounds the given line of code and catches the ClassCastException type. You can leave
your catch block empty. public void foo(Object anObject) { try { //might throw an
exception, surround me