Download CS 391 Advanced Programming in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS 391 - Advanced Programming in Java
Autumn 2006
Marks: 25
Name: __________________
Quiz 1 Solution
Roll Number: ______________
Fill in the blank spaces:
Time: 20 min.
Date: 12/09/06
[10]
1. All classes in java inherit from the Object class
2. Java code compiles into bytecode.
3. Every primitive data type has a corresponding Wrapper class.
4. In Java, memory gets allocated for objects from the heap
5. Generics provide a way for you to communicate the type of a collection to the
compiler, so that it can be checked.
6. ArrayList class in java is found in the java.util package.
7. Garbage Collection is the process of automatically freeing objects that are no longer
referenced by the program.
8. Byte Streams handle I/O of raw binary data in Java
9. Node Streams attach directly to the source/sink.
10. You need to import java.io package to use streams.
Encircle True or False:
[5]
1. Primitive data types are always passed by value in Java.
True / False
2. There is no operator overloading in Java.
True / False
3. Java allows multiple inheritance.
True / False
4. In Java, upcasting is implicit and downcasting is explicit.
True / False
5. Upcasting may result in loss of precision
True / False
Answer the following:
1.
For this problem, you should write a very simple but complete class. The class
represents a counter that counts 0, 1, 2, 3, 4,.... The name of the class should
be Counter. It has one private instance variable representing the value of the
counter. It has two instance methods: increment() adds one to the counter
value, and getValue() returns the current counter value. Write a complete
definition for the class, Counter.
[2.5]
Here is a possible answer. (Note that the initialization of the instance variable,
value, to zero is not really necessary, since it would be initialized to zero
anyway if no explicit initialization were provided.)
public class Counter {
// An object of this class represents a counter that
// counts up from zero.
private int value = 0;
// Current value of the counter.
public void increment() {
// add one to the value of the counter
value++;
}
public int getValue() {
// get the current value of the counter
return value;
}
} // end of class Counter
2.
Suppose that Fruit is the name of a class and that apricot is a variable of type
Fruit. What is the meaning of the statement "apricot = new Fruit();"?
That is, what does the computer do when it executes this statement?
[2.5]
This statement creates a new object belonging to the class Fruit, and it stores
a reference to that object in the variable apricot. More specifically, when
the computer executes this statement, it allocates memory to hold a new object
of type Fruit. It calls a constructor, which can initialize the instance variables
of the object as well as perform other tasks. A reference to the new object is
returned as the value of the expression "new Fruit ()". Finally, the
assignment statement stores the reference in the variable, apricot. So,
apricot can now be used to access the new object.
3.
What output is produced by the following program segment? (Recall that
name.charAt(i) is the i-th character in the string, name.)
[2.5]
String name;
int i;
boolean startWord;
name = "Advanced Programming In Java";
startWord = true;
for (i = 0; i < name.length(); i++) {
if (startWord)
System.out.println(name.charAt(i));
if (name.charAt(i) == ' ')
startWord = true;
else
startWord = false;
}
Output:
A
P
I
J
4.
Write a complete program that will display the first ten lines from a text file
named “file1.txt”. The lines should be written to standard output, System.out.
You can assume that the file contains at least ten lines.
[2.5]
import java.io.*;
public class FileRead
{
public static void main(String args[])
{
try {
FileReader fr
= new FileReader("File1.txt");
BufferedReader br = new BufferedReader(fr);
int count
=1;
String line;
while(((line=br.readLine() )!= null ) && (count<=10))
{
System.out.println(line);
count++;
}
br.close();
fr.close();
}
catch(IOException ioEx)
{
System.out.println("Exception: " + ioEx);
}
}
}