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, int[] numbers), that returns a string containing all the characters that appear in text at the positions specified by the numbers array. If, for some i, numbers[i] is not a valid position for string text, then that number is ignored. 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 int most_frequent(int[] data), that returns the number that appears most frequently in data. If data is empty, the function should return 0. If multiple numbers tie for most occurrences in data, then the function can return any of those numbers.

For example:


Practice Problem 10

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