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;
}
- What does foo(6, 7) return?
- What does foo(1, 2) return?
- What does foo(7, 7) return?
- What does foo(6, 4) return?
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;
}
}
- What does foo(3) return?
- What does foo(5) return?
- What does foo(7) return?
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:
- The name should have at least 4 characters, and no more than 8 characters.
- The name must start with a T.
- The only characters that are allowed to be used in the name are upper case T, Y, R, A, N. These letters can appear in any order. No lower case letters are allowed, and no other characters are allowed.
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:
- check_name("GEORGE") should return false, since the name contains illegal characters G, e, o.
- check_name("TAN") should return false, since the name has only 3 letters.
- check_name("ARAN") should return false, since the name does not start with T.
- check_name("TARAN") should return true, since the name satisfies all the rules.
Practice Problem 5
Write a function int foo(int number) that satisfies these specs:
- If there exist integers A and B, greater than or equal to 0, such that number = A*A + B*B + 3, then the function returns true.
- Otherwise, the function returns false.
For example:
- foo(3) returns true, because 3 = 0*0 + 0*0 + 3
- foo(4) returns true, because 4 = 0*0 + 1*1 + 3
- foo(37) returns true, because 37 = 3*3 + 5*5 + 3
- foo(53) returns true, because 53 = 5*5 + 5*5 + 3
- foo(6) returns false.
- foo(10) returns false.
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:
- If numbers1 = {3, -5, 0, 8, 100, 3}, then pick_positions("television", numbers1) returns "etoe".
- If numbers2 = {-5, 100, -3}, then pick_positions("television", numbers2) returns the empty string "".
Practice Problem 7
Write a function String mix(String s1, String s2), that satisfies these specs:
- If s1 does not have the same length as s2, the function returns the empty string "".
- If s1 and s2 have the same length, it returns a string that contains the characters of s1 and s2 intermingled. In other words, the result string contains the characters of s1 (in the order in which they appear in s1) in its even positions, and the characters of s2 (in the order in which they appear in s2) in its odd positions.
For example:
- mix("abcd", "1234") returns "a1b2c3d4".
- mix("aaa", "bbb") returns "ababab".
- mix("hello", "world") returns "hweolrllod".
- mix("cat", "whale") returns "".
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:
- reverse_vowels("aeiOU") returns "UOiea".
- reverse_vowels("television") returns "oiiee".
- reverse_vowels("hello world") returns "ooe".
- reverse_vowels("bcd") returns the empty string "".
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:
- If data1 = {10, 5, 2, 5, 1, 3}, then most_frequent(data1) returns 5.
- If data2 = {10, 5, 2, 5, 10}, then most_frequent(data2) may return 5 or 10.
- If data3 = {1, 2, 3, 4}, then most_frequent(data3) may return 1 or 2 or 3 or 4.
Practice Problem 10
NOTE: you do not have to write any code in this question.
- Convert binary number 10101 to decimal.
- Convert decimal number 26 to binary.
- Convert hexadecimal number 1B to decimal.
- Convert decimal number 40 to hexadecimal.