Computer Professionals Program
Master of Science in Computer Science

Maharishi University of Management, USA

Sample qualifying exam

Here is a sample test with answers in Java. The questions on the real test are similar. If you have any questions about the sample test please email your admissions representative. Please note that you will not have an opportunity to ask questions when you are taking the real online test.

Please read the list of common programming errors that students have committed on our test.

The purpose of this short test is to assess your ability to solve elementary programming problems in a language of your choice. Write your solutions in Java if you are familiar with that language; otherwise use one of these languages: C, C++, or C#. If you do not have access to a compiler for your language, write your answers in a text editor such as notepad and mention in a comment that you did not use a compiler.

For each of the problems below, write the simplest, clearest solution you can, in the form of a short program. Answer as much as you can for a problem, even if you do not have the complete answer.

You do not need to do any I/O, i.e., you can hard-code your input data and do not have to write out anything to the console. Keep it simple! We are primarily interested in what you write in the body of the function. However, please be sure that your solution will work for all valid input data.

The clock is ticking now, so you don't have time to ask for clarifications on any of the questions. If something is not clear to you, resolve it yourself and state in a comment in the program what was unclear and how you resolved it.

When you have finished an answer, copy and paste it into the text box associated with the question and click the submit button to save it in our database. If you change an answer and submit it again, the previous version of the answer will be overwritten with the new version.

1. Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.
The signature of the function is
int f(int[ ] a)

Examples:

if the input array is return
{1, 2, 3, 4} 3
{{4, 1, 2, 3}} 3
{1, 1, 2, 2} 1
{1, 1} -1
{1} -1
{} -1

Copy and paste your answer here and click the "Submit answer" button


Answer to first question

2. Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X - Y

The signature of the function is:
int f(int[ ] a)

Examples

if input array is return
{1} 1
{1, 2} -1
{1, 2, 3} 2
{1, 2, 3, 4} -2
{3, 3, 4, 4} -2
{3, 2, 3, 4} 0
{4, 1, 2, 3} -2
{1, 1} 2
{} 0

Copy and paste your answer here and click the "Submit answer" button


Answer to second question

3. Write a function that accepts a character array, a zero-based start position and a length. It should return a character array containing containing length characters starting with the start character of the input array. The function should do error checking on the start position and the length and return null if the either value is not legal.
The function signature is:
char[ ] f(char[ ] a, int start, int len)

Examples

if input parameters are return
{'a', 'b', 'c'}, 0, 4 null
{'a', 'b', 'c'}, 0, 3 {'a', 'b', 'c'}
{'a', 'b', 'c'}, 0, 2 {'a', 'b'}
{'a', 'b', 'c'}, 0, 1 {'a'}
{'a', 'b', 'c'}, 1, 3 null
{'a', 'b', 'c'}, 1, 2 {'b', 'c'}
{'a', 'b', 'c'}, 1, 1 {'b'}
{'a', 'b', 'c'}, 2, 2 null
{'a', 'b', 'c'}, 2, 1 {'c'}
{'a', 'b', 'c'}, 3, 1 null
{'a', 'b', 'c'}, 1, 0 {}
{'a', 'b', 'c'}, -1, 2 null
{'a', 'b', 'c'}, -1, -2 null
{}, 0, 1 null

Copy and paste your answer here and click the "Submit answer" button


Answer to third question

Answers

First answer

  public static void main()
  {
    a1(new int[]{1, 2, 3, 4});
    a1(new int[]{4, 1, 2, 3});
    a1(new int[]{1, 1, 2, 2});
    a1(new int[]{1, 1});
    a1(new int[]{1});
    a1(new int[]{});
  }
	
  static int a1(int[] a)
  {
    int max1 = -1;
    int max2 = -1;
		
    for (int i=0; i<a.length; i++)
    {
      if (a[i] > max1)
      {
        max2 = max1;
        max1 = a[i];
      }
      else if (a[i] != max1 && a[i] > max2)
        max2 = a[i];
    }
		
    return max2;
  }

Second answer

  public static void main()
  {
    a2(new int[] {1});
    a2(new int[] {1, 2});
    a2(new int[] {1, 2, 3});
    a2(new int[] {1, 2, 3, 4});
    a2(new int[] {3, 3, 4, 4});
    a2(new int[] {3, 2, 3, 4});
    a2(new int[] {4, 1, 2, 3});
    a2(new int[] {1, 1});
    a2(new int[] {});
  }
	
  static int a2(int[] a)
  {
    int sumEven = 0;
    int sumOdd = 0;
		
    for (int i=0; i<a.length; i++)
    {
      if (a[i]%2 == 0)
        sumEven += a[i];
      else
        sumOdd += a[i];
    }
		
    return sumOdd - sumEven;
  }

Third answer

  public static void main()
  {
    a3(new char[]{'a', 'b', 'c'}, 0, 4);
    a3(new char[]{'a', 'b', 'c'}, 0, 3);
    a3(new char[]{'a', 'b', 'c'}, 0, 2);
    a3(new char[]{'a', 'b', 'c'}, 0, 1);
    a3(new char[]{'a', 'b', 'c'}, 1, 3);
    a3(new char[]{'a', 'b', 'c'}, 1, 2);
    a3(new char[]{'a', 'b', 'c'}, 1, 1);
    a3(new char[]{'a', 'b', 'c'}, 2, 2);
    a3(new char[]{'a', 'b', 'c'}, 2, 1);
    a3(new char[]{'a', 'b', 'c'}, 3, 1);
    a3(new char[]{'a', 'b', 'c'}, 1, 0);
    a3(new char[]{}, 0, 1);
    a3(new char[]{'a', 'b', 'c'}, -1, 2);
    a3(new char[]{'a', 'b', 'c'}, -1, -2);
  }
	
  static char[] a3(char[] a, int start, int length)
  {
    if (length < 0 || start < 0 || start+length-1>=a.length)
    {
      return null;
    }
		
    char[] sub = new char[length];
    for (int i=start, j=0; j<length; i++, j++)
    {
      sub[j] = a[i];
    }
		
    return sub;
  }
back to top

Site Map