CS 1713 Introduction to Programming II Spring 2012 Daily Questions


1. Tuesday Week 1: answers

  1. Write a program segment that prints the sum of the first 1000 positive integers.

2. Thursday Week 1: answers
  1. Write a program segment that prompts the user to enter an integer
    and then sets the integer variable x to the value entered.

3. Tuesday Week 2: answers
  1. Write a code segment that will simulate the throwing
    of a standard (6-sided) die and print the result.
  2. Write a method that will simulate the throwing of a pair of dice and return the result (the sum of the values of the two dice).
    The method should have no parameters.
  3. What is the output generated by te following:
       int x = 5;
       int y = 7;
       System.out.println("The sum of "+x+" and "+y+" is "+x+y);
    
  4. What is the output of the following:
       int x = 35;
       int y = 2;
       System.out.println("The value of "+x+" divided by "+y+"+ is "+(x/y));
    
  5. Write a method that takes an array of integers as a parameter and
    prints the array in reverse order, all on one line, with consecutive
    elements separated by a single comma followed by a single blank.

5. Tuesday Week 3: answers
  1. Give an attribute for
    1. a book
    2. a student
    3. a bank account
    4. a rectangle
  2. Give a behavior for
    1. a book
    2. a student
    3. a bank account
    4. a rectangle

6. Thursday Week 3: answers
  1. What would you use for the name of the method in a Circle class that returns the circumference of the circle?
  2. Write the code for the constructor of a simple Circle class.
  3. A Triangle class has three atributes, side1, side2, and side3 which are doubles.
    Suppose we have:
    Triangle triangle1 = new Triangle(3.1,4.5,5.9);
    
    Write a code segment that prints the area of this triangle.
  4. What is printed by Example 1 in the Week 3 notes?
  5. What is printed by Example 2 in the Week 3 notes?

7. Tuesday Week 4: answers
  1. In java, the origin of the coordinate system (0,0) is at
    1. The lower left corner
    2. The upper left corner
    3. The lower right corner
    4. The upper right corner
    5. The center
  2. A rectangle is drawn using:
    g.drawRect(10,20,30,40);
    
    What are the coordinates of the upper left corner of the rectangle?
  3. A rectangle is drawn using:
    g.drawRect(10,20,30,40);
    
    What are the coordinates of the lower right corner of the rectangle?
  4. Write the code for the constructor of a simple Circle class.
  5. The Hexagon class represents a regular hexagon and has one double attribute, side.
    Suppose we have:
    Hexagon hex1 = newHexagon(5.9);
    
    Write a code segment that prints the area of this hexagon.
    (A hexagon is a six-sided figure.)


8. Thursday Week 4: answers
  1. A rectangle is drawn using:
    g.drawRect(10,20,30,40);
    
    What are the coordinates of the upper left corner of the rectangle?
  2. A rectangle is drawn using:
    g.drawRect(10,20,30,40);
    
    What are the coordinates of the lower right corner of the rectangle?
  3. What can you say about the value of "XYZ".compareTo("abc")?
    Check all that apply.
    1. It is an integer
    2. It is 0
    3. It is -1
    4. It is +1
    5. It is less than 0
    6. It is greater than 0
    7. It gives an error
  4. How many objects are created by the following code:
    int x;
    
  5. How many objects are created by the following code:
    int[] x;
    
  6. How many objects are created by the following code:
    int[] x = new int[3];
    
  7. How many objects are created by the following code:
    Square[] x;
    
  8. How many objects are created by the following code:
    Square[] x = new Square[3];
    
  9. A Fraction has a numerator and a denominator.
    Write a constructor for a Fraction class.
  10. One of the Java libraries has a class called Window that represents a window that can be displayed on the screen.
    A Window and either be visible (shown on the screen) or not visible (not shown).
    There is a method that returns true if the window is visible and false if it is not.
    What is the name of this method?

9. Tuesday Week 5: answers
  1. One of the Java libraries has a class called Window that represents a window that can be displayed on the screen.
    A Window and either be visible (shown on the screen) or not visible (not shown).
    There is a method that returns true if the window is visible and false if it is not.
    What is the name of this method?
  2. Write a method that takes a single String as a parameter and prints the string in reverse order.
  3. Write a method that takes a single String as a parameter and returns a new String which is the parameter with the order of its characters reversed.
    For example, if the function is passed the "ab cde", the returned string would be "edc ba".
  4. Write a method that takes a single String as a parameter and returns a new String which is the first token in the parameter.
    Tokens are separated by blanks.
    For example, if the function is passed
    "   abcde   fghi jkl ",
    the returned string would be "abcde".

10. Thursday Week 5: answers
  1. Fill in the blank: DrawableRectangle ______________ Rectangle
  2. Fill in the blank: DrawableRectangle ______________ DrawableShape
  3. Write a method that takes a single String as a parameter and returns a new String which is the first token in the parameter.
    Tokens are separated by blanks.
    For example, if the function is passed
    "   abcde   fghi jkl ",
    the returned string would be "abcde".

11. Tuesday Week 6: answers
  1. You are told that a certain class implements the Comparable interface.
    What does this tell you?
  2. Suppose we have:
    String s = "abcdefg";
    
    What is the value of s.substring(2)?
  3. Suppose we have:
    String s = "abcdefg";
    
    What is the value of s.substring(2,5)?
  4. Write a method that takes a single String as a parameter and returns a new String which is the first token in the parameter.
    Tokens are separated by blanks.
    For example, if the function is passed
    "   abcde   fghi jkl ",
    the returned string would be "abcde".
  5. Write the compareTo method to have studentInfo implement Comparable based on the GPA, with ties broken by the student name.
  6. Write a method that takes an array of Circle as a parameter and prints the radius of the largest circle.

14. Thursday Week 7: answers
  1. What is an exception?
  2. What is the difference between a checked exception and an unchecked exception?
  3. Suppose s is a String that represents an integer,
    such as s = "1234";
    Write a code segment that sets the integer variable x
    to the corresponding value. You don't have to do any error checking.
  4. Suppose s is a String that represents an integer,
    such as s = "1234";
    Write a code segment that sets the integer variable x
    to the corresponding value, or -1 if s does not represent a valid integer.

15. Tuesday Week 8: answers
  1. What is the value of log2 32?
  2. Write a method that take an array of String, list, and a String, target, as parameters and returns the position in the array of target, or -1 if it is not in the array.
  3. Suppose the method in the previous problem is called with an array of size 1,000,000.
    What is the average number of String comparisons that need to be made when searching for a target that is in the array?
  4. Suppose a telephone directory has 1,000,000 entries. You need to look up the phone number of someone who is listed in the book. On the average, how many entries in the book do you need to look at to find the correct entry?

16. Thursday Week 8: answers
  1. Write a code segment that uses a StringTokenizer to print the first token in the String s.
  2. Give the closest integer approximation to log2 1,000,000.
    Do not use a calculator.
  3. A binary search is done on a list of size 1,000,000. What is the maximum number of times that the body of the loop will be executed?
  4. Write a method that takes an array of String, list, and an integer, start, as parameters and returns the position in the array of the smallest element of the part of the list starting at position start. Assume that this part of the list has at least one element.
  5. Suppose the method from the previous problem is called with a list of size 100 and start equal to 23. How many string comparisons are done by the method?

17. Tuesday Week 9: answers
  1. Give the closest integer approximation to log2 4,000,000.
    Do not use a calculator.
  2. Suppose it takes 10 milliseconds on the average
    to do a binary search on a sorted array of size 1,000,000.
    How long would it take on a similar array of size 2,000,000?
    1. 5 milliseconds
    2. 10 milliseconds
    3. 10.5 milliseconds
    4. 11 milliseconds
    5. 20 milliseconds
  3. Suppose it takes 10 seconds on the average
    to do a linear search on an array of size 1,000,000.
    How long would it take on a similar array of size 1,000,000,000?
    1. 10 seconds
    2. 11 seconds
    3. 15 seconds
    4. 20 seconds
    5. a few minutes
    6. a few hours
    7. a few weeks
    8. a few months
    9. a few years
  4. Suppose it takes 10 milliseconds on the average
    to do a binary search on a sorted array of size 1,000,000.
    How long would it take on a similar array of size 1,000,000,000?
    1. 10 milliseconds
    2. 11 milliseconds
    3. 15 milliseconds
    4. 20 milliseconds
    5. A little less than a second
    6. a few seconds
    7. a few minutes
    8. a few hours
    9. a few weeks
    10. a few months
    11. a few years
  5. Suppose it takes 10 seconds on the average
    to do selection sort on an array of size 1,000,000.
    How long would it take on a similar array of size 1,000,000,000?
    1. 10 seconds
    2. 11 seconds
    3. 15 seconds
    4. 20 seconds
    5. a few minutes
    6. a few hours
    7. a few weeks
    8. a few months
    9. a few years

18. Thursday Week 9: answers
  1. Give the closest integer approximation to log2 500,000.
    Do not use a calculator.
  2. Suppose it takes 10 milliseconds on the average
    to do selection sort on an array of size 1,000.
    How long would it take on a similar array of size 1,000,000,000?
    1. 10 seconds
    2. 11 seconds
    3. 15 seconds
    4. 20 seconds
    5. a few minutes
    6. a few hours
    7. a few weeks
    8. a few months
    9. a few years
    10. many years
  3. Declare a variable, myList, that can hold an ArrayList of String.
  4. Write a method that takes an ArrayList of String as a parameter and prints the elements, one per line.
  5. Suppose you were given the code (in eclipse) for a selection sort on an array of String.
    How long would it take you to convert it to a selection sort on an ArrayList of String?

19. Tuesday Week 10: answers
  1. Give the closest integer approximation to log2 2,000,000.
    Do not use a calculator.
  2. Suppose it takes 10 microseconds on the average
    to do a binary search on an array of size 1,000.
    How long would it take on a similar array of size 1,000,000,000?
    Note: a microsecond is a millionth of a second.
    1. 10 microseconds
    2. 20 microseconds
    3. 30 microseconds
    4. 100 microseconds
    5. a few milliseconds
    6. a few seconds
    7. a few minutes
    8. a few hours
    9. a few days
    10. a few weeks
    11. a few months
    12. a few years
    13. many years
  3. Declare a variable, myList, that can hold an ArrayList of String.
  4. Write a method that takes an ArrayList of String as a parameter and prints the first element that begins with a vowel. The vowels are a, e, i, o, and u, both lower and upper case.

20. Thursday Week 10: answers
  1. Fill in the blank: A _____________ layout manager organizes components, either vertically or horizontally,
    in one row or column.
  2. Give the closest integer approximation to log2 7,000,000.
    Do not use a calculator.
  3. Suppose it takes 10 microseconds on the average
    to do a selection sort on an array of size 1,000.
    How long would it take on a similar array of size 1,000,000,000?
    Note: a microsecond is a millionth of a second.
    1. 10 microseconds
    2. 20 microseconds
    3. 30 microseconds
    4. 100 microseconds
    5. a few milliseconds
    6. a few seconds
    7. a few minutes
    8. a few hours
    9. a few days
    10. a few weeks
    11. a few months
    12. a few years
    13. many years
  4. Declare a variable, myList, that can hold an ArrayList of String.
  5. Write a method that takes an ArrayList of String as a parameter and prints the first element that begins with a vowel. The vowels are a, e, i, o, and u, both lower and upper case.

21. Tuesday Week 11: answers
  1. Declare an ArrayList variable, myNumbers, that can hold
    a list of RationalNumber objects.
  2. True or false: An array is an object.
  3. Write a method that takes an array of double as a parameter
    and retruns a new array that contains only those elements
    of the array parameter that are greater than 0.
  4. Write a method that takes an ArrayList of String as a parameter and prints the first element that begins with a vowel. The vowels are a, e, i, o, and u, both lower and upper case.

24. Thursday Week 12: answers
  1. Write a method that takes an array of char as a parameter
    and returns true if all of the elements of the array are the same.
    It returns false otherwise.
  2. Write a method that takes an array of String as a parameter and returns
    true if all of the characters of at least one string in the array are the same.
    It returns false otherwise.

25. Tuesday Week 13: answers
  1. Write a method that takes a single String parameter and returns an int. If the parameter represents a valid positive integer, return that value. Otherwise, return -1.
  2. Write a method that takes an array of char as a parameter and returns
    true if all of the characters in the array are 'X' or if all of the characters in the array are 'O'. Otherwise, it returns false.
  3. Write a method:
    public static boolean rowWin(char[][] board, int n, char c);
    
    that returns true if row n of board is filled with c , and false otherwise. Assume that board has a row n.
  4. Write a method:
    public static boolean colWin(char[][] board, int n, char c);
    
    that returns true if column n of board is filled with c , and false otherwise. Assume that each row of board has a column n.

26. Thursday Week 13: answers
  1. Write a method that takes a 2-dimensional array of int as a parameter and returns the maximum value stored in the array. If the array is empty, return Integer.MIN_VALUE
  2. Write a method:
    public static boolean rowWin(char[][] board, int n, char c);
    
    that returns true if row n of board is filled with c , and false otherwise. Assume that board has a row n.
  3. Write a method that takes a 2-dimensional array of double as a parameter and returns the number of elements in the array.

27. Tuesday Week 14: answers
  1. Write a method that takes a 2-dimensional array of int as a parameter and returns the largest positive value stored in the array. If the array has no positive values, return -1.
  2. Assume that X_CHAR, O_CHAR, DRAW_CHAR, and EMPTY_CHAR have been appropriately defined. Write a method that takes a square 2-dimensional array of char as a parameter, interpreted as a Tic Tac Toe board, and returns X_CHAR if it is X's turn, O_CHAR if it is O's turn, and DRAW_CHAR if the game is a draw.

28. Thursday Week 14: answers
  1. Write a method that takes a 2-dimensional array of int as a parameter and returns the number of elements in the array that are greater than zero.
  2. Write a method that takes a String and a char as parameters and returns the number of times the char occurs in the String.
  3. Write a method that takes a single String parameter and returns a String. If the parameter does not start and end with a double quote character ("), just return the parameter, unchanged. Otherwise, return the part of the parameter between the quotes.

29. Tuesday Week 15: answers
  1. Write a method that takes a String as a parameter and returns true if the characters are in alphabetical order. Otherwise it returns false. Duplicates are allowed.