MidTerm Questions for First Course in Java  

course homepage      course calendar

SYNTAX

  1. In the following code, is what is the datatype of argument to the method?
    System.out.println("4");

     
  2. Which of the following is (are) correct?
  1. Which of the following is (are) correct?
  1. Which of the following is (are) correct?

 

  1. True or False: To override a method means that there is more than one version of that method.
     
  2. Correct the following statement: The signature of a method is the name of the method, the number of arguments you pass to that method, the argument types, and the argument names.
     
  3. Suppose you have an local variable, myVar, that hides an instance variable of the same name. Is there any way to access the instance variable? If so, how?
     
  4. A block of code defines the ___________ of a variable.
     
  5. Suppose you have an instance of the class Employee called employee, and this class has a method, takeSickDay(). This method takes as its argument an integer value of days. Write the code to let the employee take 3 sick days off to go gamble in Reno.

    ____________________________
     
  6. True or False: The java compiler returns an error if a class with a method called myMethod() inherits from a superclass that also has a method called myMethod().
     
  7. Name one advantage and one disadvantage of object-oriented programming.
    Advantage:

    Disadvantage:


     
  8. Which of the following is (are) a valid Java statement:
  1. True or False: The primitive data types in Java are: char, string, float, and int.
     
  2. Write the code to create an instance dirtBike of the class Bicycle.
     
  3. True or False: In Java, boolean tests return the string value "true" or "false".
     
  4. True or False: The Java Virtual Machine (JVM) can run source code for any platform.
     
  5. What makes an applet different from an application?
     
  6. In what sense is a class like a template?
     
  7. Write the code to create an array of 12 colors and assign the third color to be orange.
     
  8. What is the difference between an interface and an implementation?
     
  9. What is the output of the following arithmetic operation:
    int sum = (int) 10.0 / (int) 5.1;
     
  10. What does the compiler look for to distinguish between a char and a string?
     
  11. True or False: Java requires that each value in an array have a data type that distinguishes it from the other values in the array.
     
  12. Which keyword distinguishes an instance variable from a class variable?
     
  13. Why might you want to create a class variable? 
     
  14. Write the code to cast the double myDouble as a float myFloat.
     
  15. Correct the three errors in the following code:
    int temperature = 70; // Fahrenheit
    int drunk = .01; // in percent
    int BOILING_POINT = 212; // a constant
    String[] myDays = {'Sunday', 'Monday', 'Tuesday'};
     
  16. Typically, Java programmers follow conventions. Which of the following fail to uphold the conventions:
    MyVariable
    MYCONSTANT
    MyMethod()
    MyClass
     
  17. True or False: It is possible to multiply and divide efficiently by shifting bits.
     
  18. Write code to implement the logic in this pseudocode:
    If today is Saturday, go to the park. If today is Sunday, go to the movies. If today is any other day, go to work. 
     
  19. Write code to implement the logic in this pseudocode:
    Pour orange juice into paper cups until there are no more paper cups or the boss says "Stop".
     
  20. What is the shortcut way to write x = x/y ?
     
  21. Write code to express this concept: 
    My variable modulus your variable does not equal the return value of the method getTotal().
     
  22. When you see the word "implements" instead of "extends", what does it mean?
     
  23. Write an interface for ProfessionalAthlete and define its three methods.
     
  24. What might be a typical attribute and a typical method for a class called AirlinePilot?
     
  25. Specify three differences between an abstract class and an interface?
     
  26. Fill in the blanks (you might have to put 0, 1 or more words in the blanks):

    public _____ Person

    {

        private _____ firstName;

        private _____ lastName;

        private Date birthDate;

     

    /**

    * This is the constructor for the class Person

    * it takes the name as parameter

    */

    public _____ Person(String firstName, String lastName, Date birthDate)

    {

        ___________ = firstName;

        this.lastName = __________;

       ___________ = birthDate;

    }

     

    /**

    * This method returns the first name of the person

    * followed by the last name.

    */

    public String getName()

    {

        return _________________________;

    }

    }

  27. Fill in the blanks (you might have to put 0, 1 or more words in the blanks):

    public interface Shape

    {

        public _______ void draw(Graphics g);

        public ______ ______ getArea();

       public ______ ______ getPerimiter();

    }

     

  28. Fill in the blanks (you might have to put 0, 1 or more words

    in the blanks):

    // in the file MyGreatExample.java

    ______ ______________ MyGreatExample

    {

        ________ String value;

     

        private MyGreatExample(String _____, String ______)

    {

        value = value1 + value2;

        System.___.println("Hello, your are initializing an

        instance of MyGreatExample!");

    }

     

    public MyGreatExample()

    {

        this("A name", _________);

    }

     

    public abstract void doSomething();

     

        public _______ computeValue()

    {

        return 1.0 + 3.14;

    }

    public _______ computeValue()

    {

        return 1.0 + 3.14;

    }

    }

    // still in the same file!

    class MyOtherClass _________ MyGreatExample()

    {

    public MyOtherClass()

    {

    ______("one", "two");

    }

     

    public void doSomething()

    {

        return _______;

    }

    }

     

  29. Mark and correct the different errors in this code:

    public abstract class MyClass

    {

    private String name = 1;

    public Myclass()

    {

    }

     

    public void getName()

    {

    return name;

    }

     

    public String getSomethingElse()

    {

    // do something here

     

    // and do more around here

    return;

    }

     

    public void setName(String name)

    {

    name = name;

    }

     

    public void doSomethingElse(int somethingElse)

    {

    somethingElse += (somethingElse / somethingElse);

    return somethingElse;

    }

    }

    5. mark and correct the different errors in this code:

    public class TestClass

    {

    private TestClass()

    {

    }

     

    public static void main(String args[])

    {

    for (i = 0; true; i ++)

    {

    if (i > 54)

    break;

    if (i < 10)

    System.out.println("I am happy.");

    if ( (i > 10) & (i < 30))

    System.out.println("I am still happy.");

    else

    {

    System.out.println("Okay, I am annoyed")

    }

    }

    }

    }

  30. What is the output of the method convert for an input value of:

    - 0

    - 1

    - 67

    - 3

    - 5

    - 6

    - 2

    public class TestClass2

    {

    public int convert(int value);

    {

    int returnValue = 0;

     

    switch (value)

    {

    case 0:

    returnValue = value;

    break;

    case 5:

    returnValue = value+3;

    case 3:

    case 6:

    case 9:

    return value;

    case 2:

    break;

    case 1:

    returnValue = value*value;

    default:

    returnValue = value * returnValue;

    break;

    }

     

    return returnValue;

    }

    }

    8. what is the output of the hello method for the following inputs:

    value1 = 3; value2 = 5;

    value1 = 0; value2 = 3;

    value1 = 5; value2 = 2;

    public class MyHelloTest

    {

    public int hello(int value1, int value2)

    {

    while (true)

    {

    value1 += value2;

    if ((value1 % 3) != 0)

    return value2;

    if ((value2 % 3) == 1)

    value1 += 10;

    else if (value1 > 23)

    break;

    if (value1 == value2)

    return 123;

    }

     

    for (int i = 0; i < 3; i++)

    {

    value2 += value2;

    }

     

    switch (value1)

    {

    case 1:

    case 5:

    case 10:

    case 23:

    case 24:

    return value1;

    case 33:

    break;

    default:

    value2 = 67;

    break;

    }

     

    return 0;

    }

    }

_____________________
course homepage      course calendar