INDA Quiz #1
November 3, 1999
Solutions


There are 30 multiple choice questions on the quiz.  Each question has exactly one answer and is worth one point.  Clearly draw a circle around the number of the answer of your choice for each question.

You will have one hour and forty five minutes to complete the quiz.

NOTE:  In some questions, you are given several statements that might all be true.   In that case, there will also be a choice to specify a group of statements.   For example, if statements 1 and 2 are both true, then there will be a choice of the form "1 and 2".  In this case, you must choose the statement that says "1 and 2".  If you choose 1 and it turns out that 2 was also true and you missed the "1 and 2" choice, then you will be marked wrong and get no credit.

Remember, this quiz is strictly voluntary.  There is no cutoff for passing the quiz.  You can use the points for the tenta or not as you wish (according the rules specified on the web site).  So, you have absolutely nothing to loose here and everything to gain.  In other words, just relax and do the best you can.

You may have one elementary Java text book with you as well as an English-X dictionary (where X is the language of your choice).

Good luck!


1.  The difference between the types int and long is

  1. long can hold only floating numbers
  2. long can hold both integer and floating point numbers
  3. long can hold larger integer numbers than int
  4. none of the above

Ans:  3


2.  Consider the following code fragment

double d = 26.99;
int i;
int n;
i = (int) d;
n = (int)(-d);

  1. the value of i is 26 and the value of n is -27
  2. the value of i is 26 and the value of n is 26
  3. the value of i is 26 and the value of n is -26
  4. the value of i is 27 and the value of n is -27
  5. the value of i is 27 and the value of n is -26
  6. none of the above

Ans:  3


3.  Consider the code fragment

char c = '2';
int i = (int)c;

Which of the following statements is correct.

  1. Casting type char to int is not legal.
  2. The value of i is 2.
  3. The value of i is an integer, but it is not 2.
  4. None of the above.

Ans:  3


4.  The difference between values of type float and type double is

  1. There is no difference.
  2. Both hold exact values.
  3. floats hold exact values while doubles hold approximate values.
  4. doubles hold exact values while floats hold approximate values.
  5. none of the above

Ans:  5


5.  If the length of a String variable is 10, the index of its last character is

  1. 0
  2. 9
  3. 10
  4. 11
  5. none of the above

Ans:  2


6.  Given a class MyExample, which has an instance variable called myData. How many copies of myData are stored in memory?

  1. Zero
  2. One
  3. The number of times the value of myData is changed.
  4. The total depends on the number of MyExample objects that have been created.
  5. 3

Ans:  4


7.  Suppose the method getName() in the class MyExample returns type String and let anExample be an object of the class MyExample.  Which of the following statements is legal?

  1. String s = anExample.getName();
  2. int string = anExample.getName();
  3. int string = anExample.getName;
  4. MyExample me = anExample.getName();
  5. char c = anExample.getName();

Ans:  1


8.  A variable that is used inside a method can be:

  1. An instance variable.
  2. A local variable of that method.
  3. A class variable.
  4. 1 and 2
  5. 1, 2 and 3
  6. 2 and 3

Ans:  5


9.  Consider the following class:

class MyExample
{
  public void myMethod(char letter)
  {
    letter = 'A';
  }
  public static void main(String[ ] args)
  {
    MyExample myEx = new MyExample();
    myEx.myMethod(myEx.ch);
    System.out.println(myEx.ch);
  }
  public char ch = 'B';
}

     When we run the program, the output is:

  1. B
  2. There is no output since the code will not compile.
  3. A

Ans:  1


10.  Which of the following comments does not apply to information hiding?

  1. It is an abstraction mechanism.
  2. It is achieved, in part, by making instance variables private.
  3. It prevents "information overload".
  4. It makes it easier for programmers to work in teams.
  5. It makes it harder to call a method because the method's details are hidden.

Ans:  5


11.  Consider the following code fragment:

int x;
x=SavitchIn.readLineInt();
if (x >= 0)
  System.out.println(x);

       Which of the following statements is true?

  1. If the value entered for x is negative no output occurs.
  2. The code fragment will not compile because there is no else.
  3. If the value entered for x is zero no output occurs.
  4. If the value entered for x is positive no output occurs.
  5. None of the above.

Ans:  1


12.  Which of the following is NOT a valid boolean expression:

  1. (0 > x) && (-5 < x)
  2. 0 > x > -5
  3. (0 > x) && (-5 > x)
  4. x > -5
  5. (0 > x) && (x > -5)

Ans:  2


13.  Consider the following code fragments:

(i) if (x > 0) && (x < 10)
  System.out.println("x is between 0 and 10");

(ii) if (x > 0 && x < 10)
  System.out.println("x is between 0 and 10");

       Which of the following statements is true?

  1. The two fragments give the same output
  2. The second fragment will not compile
  3. Neither fragment will compile
  4. The first fragment will not compile

Ans:  4


14.  Consider the following code fragment:

int x;
x = SavitchIn.readLineInt( );
if (x >= 0)
{
  System.out.println("positive input:");
  System.out.println(x);
}
else
  System.out.println("negative input:");
  System.out.println(x);

      Which of the following statements is true?

  1. The code fragment will not compile.
  2. If the value entered for x is positive, "positive input:" only is
    displayed.
  3. If the value entered for x is zero no output occurs.
  4. If the value entered for x is positive, "positive input:" is
    displayed, followed by the value of x, displayed once.
  5. If the value entered for x is positive, "positive input:" is
    displayed, followed by the value of x, displayed twice.
  6. None of the above

Ans:  5


15.  Consider the following code fragment:

int x = 0;
while (x < 10)
  x++;
  System.out.println(x);

       Which of the following statements is correct?

  1. 10 different values of x will be printed
  2. no values of x will be printed
  3. 9 different values of x will be printed
  4. one value of x will be printed
  5. 10 equal values of x will be printed
  6. the code fragment results in an infinite loop.

Ans:  4


16.  Consider the following statement, which is a method call: 

myMethod(); 

       Which of the following is correct?

  1. It is valid no matter where it occurs.
  2. It is invalid because there is no argument.
  3. It is never valid unless this. is used.
  4. It is valid if it occurs within the body of a (nonstatic) method of the same class.
  5. It is invalid because there is no calling object.

Ans:  4


17.  The construction this. can be used:

  1. As a prefix to a method call.
  2. As a prefix to an instance variable.
  3. As a prefix to a local variable.
  4. 1 and 2
  5. 2 and 3
  6. None of the above.

Ans:  4


18.  Which of the following statements is true?

  1. Only classes without instance variables may have static methods.
  2. A class cannot have BOTH static methods and instance methods.
  3. Only user-defined classes may have static methods.
  4. Only wrapper classes may have static methods.
  5. Any class may have static methods.

Ans:  5


19.  Consider the following method headings:

(i) public int myMethod(int a, char b)
(ii) public void myMethod()
(iii) public int myMethod(int a)
(iv) public int myMethod(char b, int a)
(v) public int myMethod(int b, char a)
(vi) public void myMethod(int a, char b)
(vii) public void myMethod(int a, char b, float c)

      Which of the following combinations could appear together in the same class?

  1. Any one (but only one) can be used.
  2. All except (vi) can be used together.
  3. All seven can be used together.
  4. All except (i) can be used together.
  5. (ii), (iii), (vi), and (vii) can be used together.
  6. All except (v) can be used together.

Ans:  5


20.  Suppose there is a class MyExample, which has ONLY the following constructors:

public MyExample(int a)
public MyExample(double f, char c)

Which of the following statements is false:

  1. MyExample myEx = new MyExample(); is a valid object declaration.
  2. MyExample myEx = new MyExample(7); is a valid object declaration.
  3. MyExample myEx = new MyExample(3.1, 'A'); is a valid object declaration.
  4. MyExample myEx = new MyExample(7, 'a'); is a valid object declaration.
  5. MyExample myEx = new MyExample(); is not a valid object declaration.
  6. 2, 3 and 4.

Ans:  1


21.  Which of the following statements are true?

  1. A derived class may have more public methods than its base class
  2. A derived class may have fewer public methods than its base class
  3. A derived class always has the same number of public instance methods as its base class
  4. 1 and 2

Ans:  1


22.  Consider the following classes:

public class Base
{
  public void print(int num)
  {
    System.out.println(num);
  }
}
public class Derived extends Base
{
  public void print(String s)
  {
    System.out.println(s);
  }
}

Which of the following is true?

  1. The print method in the class Base is overridden in the class Derived.
  2. The print method in the class Base is overloaded in the class Derived.
  3. An object of class Derived can be the calling object of both print methods,
  4. An object of class Base can be the calling object of both print methods.
  5. 2 and 3
  6. all of the above

Ans:  5


23.  Consider the following classes:

public class Base
{
  public void print()
  {
    System.out.println(num);
  }
  public void setNum(int j)
  {
    num = j;
  }
  private int num = 10;
}
public class Derived extends Base
{
  public void setNum(int j)
  {
    num = j + 10;
  }
  public static void main(String[] args)
  {
    Derived d = new Derived();
    d.setNum(20);
    d.print();
  }
}

Which of the following is true?

  1. The output of the program will be 30
  2. The program will not compile
  3. The output of the program will be 10
  4. The program will compile but there will be a run-time error
  5. The output of the program will be 20

Ans:  2


24.  Consider the following classes:

public class Base
{
  public void print(Base b)
  {
    System.out.println("Base");
  }
}
public class Derived extends Base
{
  public void print(Derived b)
  {
    System.out.println("Derived");
  }
  public static void main(String[] args)
  {
    Base d1 = new Derived();
    Derived d2 = new Derived();
    d1.print(d1);
    d1.print(d2);
  }
}

Which of the following is correct:

  1. The program displays "Derived" on one line and "Base" on the next line
  2. The program displays "Base" twice on separate lines
  3. The program displays "Derived" twice on separate lines
  4. The program displays "Base" on one line and "Derived" on the next line
  5. The program will not compile because "d1.print(d2)" is illegal

Ans:  2


25.  In the definition of methods in a derived class the method name super

  1. refers to a constructor of the base class
  2. refers to the calling object.
  3. refers to the operating system.
  4. refers to a constructor of the derived class (i.e., the class being defined).
  5. None of the above.

Ans:  1


26.  Which of the following statements about the concept of a stream in Java is true?

  1. A stream is an object which represents a flow of data.
  2. An input stream provides data to a program as it is executing.
  3. An output stream can be generated by a program.
  4. System.out is an output stream object.
  5. All of the above.
  6. 2 and 3

Ans:  5


27.  Which of the following statements is true?

  1. Text files do not use binary digits ( 0 and 1) to store data.
  2. A computer can read data from a binary file faster than from a text file.
  3. A program can write to a text file but cannot read from a text file.
  4. Both text files and binary files are easily read by humans.
  5. A program can read from a binary file but cannot write to a binary file.
  6. None of the above is true.

Ans:  2


28.  Suppose a class contains the following declaration:

DataOutputStream myFile =
new DataOutputStream(new FileOutputStream("file.dat"));

Which of the following statements is true?

  1. myFile is the name of a stream.
  2. file.dat is the name of a stream.
  3. The declaration connects the real file name, file.dat, to the object myFile
  4. myFile and file.dat can be used interchangeably.
  5. 1 and 3
  6. 1, 3, and 4

Ans:  5


29.  Which of the following statements is true?

  1. Path separators are always "\" on all computer systems that can run Java
  2. Path separators are always "/" on all computer systems that can run Java
  3. For most programming languages, binary files are platform independent.
  4. Binary files must not have the extension "txt".
  5. Text file names must always have extension "txt".
  6. None of the above.

Ans:  6


30.  Which of the following types can be written to an object of the class DataOutputStream?

  1. all primitive types and all class types
  2. String only
  3. primitive types only
  4. char and String only
  5. all the primitive types and String
  6. char only

Ans:  5