from  Computer Professionals Master’s Program Sample Test

68 篇文章 2 订阅

                           today,I take a test  :   from  Computer Professionals Master’s Program Sample Test

 form:   https://compro.mum.edu/sample-test/

Computer Professionals
Master’s Program Sample Test

You will be asked to complete a real test as part of your application process.

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#. For each of the problems below, write the simplest, clearest solution you can, in the form of a short program.

SAMPLE TEST

  1. An array with an odd number of elements is said to be centered if all elements (except the middle one) are strictly greater than the value of the middle element. Note that only arrays with an odd number of elements have a middle element. Write a function that accepts an integer array and returns 1 if it is a centered array, otherwise it returns 0.

Examples:

if the input array isreturn
{1, 2, 3, 4, 5}0 (the middle element 3 is not strictly less than all other elements)
{3, 2, 1, 4, 5}1 (the middle element 1 is strictly less than all other elements)
{3, 2, 1, 4, 1}0 (the middle element 1 is not strictly less than all other elements)
{1, 2, 3, 4}0 (no middle element)
{}0 (no middle element)
{10}1 (the middle element 10 is strictly less than all other elements)

 

private static int Science1(int[] c)
		{
			 
			if (c.Length == 0) return 0;
			if (c.Length == 1) return c[0];
			if (c.Length / 2 == 0) return 0;
			int res = 1;
			int pps = c.Length / 2 ;
			for (int i = 0; i < c.Length; i++)
			{
				if (i==pps)
				{
					 
				}
				else
				{
					if (c[i]<=c[pps])
					{
						res = 0;
						break;
					} 
				}
			}
			return res;
		}

 

 See correct answers to sample questions.

 

  1. 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 isreturn
{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

 

private static int Science2(int[] c)
		{
			if (c.Length == 0) return 0;
			if (c.Length == 1) return c[0];
			int sumJ = 0;
			int sumO = 0;
			for (int i = 0; i < c.Length; i++)
			{
				
				if (c[i] % 2 == 0) {
					sumO += c[i];
				}
				else
				{
					sumJ += c[i];
				}
			}
			int res = sumJ - sumO;
			return res;
		}

 See correct answers to sample questions.

 

  1. Write a function that accepts a character array, a zero-based start position and a length. It should return a character array containing containing lengthcharacters starting with the startcharacter 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 arereturn
{‘a’, ‘b’, ‘c’}, 0, 4null
{‘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, 3null
{‘a’, ‘b’, ‘c’}, 1, 2{‘b’, ‘c’}
{‘a’, ‘b’, ‘c’}, 1, 1{‘b’}
{‘a’, ‘b’, ‘c’}, 2, 2null
{‘a’, ‘b’, ‘c’}, 2, 1{‘c’}
{‘a’, ‘b’, ‘c’}, 3, 1null
{‘a’, ‘b’, ‘c’}, 1, 0{}
{‘a’, ‘b’, ‘c’}, -1, 2null
{‘a’, ‘b’, ‘c’}, -1, -2null
{}, 0, 1null
private static char[] Science3(char[] c, int start, int len)
		{
			char[] res = new char[] { };
			if (c.Length == 0) return null;
			if (c.Length == 0) return res;
			if (len < 0 || start < 0) return null;
			if (len - start > c.Length) return null; 
			string s = "";
			for (int i = 0; i < c.Length; i++)
			{
				s += c[i];
			} 
			string resnew= s.Substring(start, len);
			res = resnew.ToCharArray();
			return res;
		} 

 See correct answers to sample questions.

 

  1. Write a function to reverse an integer using numeric operators and without using any arrays or other data structures.
    The signature of the function is:
    int f(int n)

Examples

if the input integer isreturn
12344321
1200550021
11
10001
00
-12345-54321
private static int Science4(int x)
		{
			if (x == 0) return 0;
			bool flag = x > 0;
			int value = flag ? x : -x; //保证是正数开始下面的循环
			long result = 0;
			while (value>0)
			{
				int num = value % 10;
				value = value / 10;
				result = result * 10 + num;
				if (result > int.MaxValue) return 0;
			}
			return (int)(flag ? result : -result);//转回负数
		}

 See correct answers to sample questions.

 

  1. Write a function to return an array containing all elements common to two given arrays containing distinct positive integers. You should not use any inbuilt methods. You are allowed to use any number of arrays.
    The signature of the function is:
    int[] f(int[] first, int[] second)

Examples

if input parameters arereturn
{1, 8, 3, 2}, {4, 2, 6, 1}{1, 2}
{1, 8, 3, 2, 6}, {2, 6, 1}{2, 6, 1}
{1, 3, 7, 9}, {7, 1, 9, 3}{1, 3, 7, 9}
{1, 2}, {3, 4}{}
{}, {1, 2, 3}{}
{1, 2}, {}{}
{1, 2}, nullnull
null, {}null
null, nullnull

 

private static int[] Science5(int[] c1, int[] c2)
		{
			string sb = "";
			for (int i = 0; i < c1.Length; i++)
			{
				for (int j = 0; j < c2.Length; j++)
				{
					if (c1[i]==c2[j])
					{
						sb += c1[i];
					}
				}
			}
			char[] res = sb.ToArray();
			int len = res.Length;
			int[] c = new int[len];
			for (int i = 0; i < len; i++)
			{
				int asas = int.Parse(res[i].ToString());
				c[i] = asas;
			}
			return c; 
		}

 See correct answers to sample questions.

 

  1. Consider an array A with n of positive integers. An integer idx is called a POE (point of equilibrium) of A, if A[0] + A[1] + … + A[idx – 1] is equal to A[idx + 1] + A[idx + 2] + … + A[n – 1]. Write a function to return POE of an array, if it exists and -1 otherwise. 
    The signature of the function is:
    int f(int[] a)

Examples

if input arrays arereturn
{1, 8, 3, 7, 10, 2}3 Reason: a[0] + a[1] + a[2] is equal to a[4] + a[5]
{1, 5, 3, 1, 1, 1, 1, 1, 1}2 Reason: a[0] + a[1] is equal to a[3] + a[4] + a[5] + a[6] + a[7] + a[8]
{2, 1, 1, 1, 2, 1, 7}5 Reason: a[0] + a[1] + a[2] + a[3] + a[4] is equal to a[6]
{1, 2, 3}-1 Reason: No POE.
{3, 4, 5, 10}-1 Reason: No POE.
{1, 2, 10, 3, 4}-1 Reason: No POE.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值