CSE 1310 - Practice Problems - Practice Set 2


Practice Problem 1

public class question1
{
  public static int foo(int a, int b, int c)
  {
    System.out.printf("1: a = %d\n", a);
    System.out.printf("2: b = %d\n", b);
    System.out.printf("3: c = %d\n", c);
    a = 2*a;
    b = c;
    c = a - 1;
    System.out.printf("4: a = %d\n", a);
    System.out.printf("5: b = %d\n", b);
    System.out.printf("6: c = %d\n", c);
    return a+b+c;
  }

  public static void main(String[] args)
  {
    int a = 3;
    int b = 4;
    int c = 1;
    c = foo(b, a, c);
    System.out.printf("7: a = %d\n", a);
    System.out.printf("8: b = %d\n", b);
    System.out.printf("9: c = %d\n", c);
  }
}
What does this program print?


Practice Problem 2

  public static int foo(int a, int b)
  {
    if ((a == 7) || (b == 2))
    {
      return 1;
    }
    if ((a == 7) && (b == 7))
    {
      return 2;
    }
    int result = 0;
    for (int i = a; i <= b; i++)
    {
      result = result + i;
    }
    return result;
  }


Practice Problem 3

  public static int foo(int a)
  {
    if (a == 5)
    {
      return 1;
    }
    else if (a < 5)
    {
      return foo(a+1)*2;
    }
    else
    {
      return foo(a-1)*3;
    }
  }


Practice Problem 4

In the medieval kingdom of Tyran, the ruler has decreed that all newborn babies should receive names that satisfy all the following rules: Write a function check_name(String name) that returns boolean value true if and only if the name satisfies all the rules stated above, and returns false otherwise. For example:

Practice Problem 5

Write a function int foo(int number) that satisfies these specs: For example:


Practice Problem 6

Write a function pick_positions(String text, String positions), that returns a string containing all the characters that appear in text at the positions specified by the string called positions. If some number in positions is not a valid position for string text, then that number is ignored.

You can assume that all numbers in positions are separated from each other by a single space character, i.e., a single ' ' character.

For example:


Practice Problem 7

Write a function String mix(String s1, String s2), that satisfies these specs: For example:


Practice Problem 8

Write a function String reverse_vowels(String text), that returns a string containing all the vowels in text, in REVERSE order compared to how they appear in text.

Note that vowels are the upper-case and lower-case versios of A, E, I, O, U. For example:


Practice Problem 9

Write a function char most_frequent_vowel(String text), that returns the vowel that appears most frequently in text. If text is empty, or if text contains no vowels, the function should return the space character ' '. If multiple vowels tie for most occurrences in text, then the function can return any of those vowels.

Comparisons should be case-insensitive, so 'A' and 'a' should be treated as the same vowel. The function should return an upper-case vowel.

For example:


Practice Problem 10

NOTE: you do not have to write any code in this question.