Maharishi University of Management (MSc Computer Science test questions)
Upcoming SlideShare
Loading in...5
×
 

Maharishi University of Management (MSc Computer Science test questions)

on

  • 2,737 views

Maharishi University of Management (MSc Computer Science test questions)

Maharishi University of Management (MSc Computer Science test questions)

Statistics

Views

Total Views
2,737
Views on SlideShare
2,736
Embed Views
1

Actions

Likes
1
Downloads
82
Comments
0

1 Embed 1

http://www.docseek.net 1

Accessibility

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Maharishi University of Management (MSc Computer Science test questions) Maharishi University of Management (MSc Computer Science test questions) Document Transcript

  • MUM TEST1. Write a function that accepts an array of non-negative integers and returns the second largestinteger in the array. Return -1 if there is no second largest. You may assume that the input array hasno negative values in it.If you are programming in Java or C#, the signature of the function isint f(int[ ] a)If you are programming in C or C#, the signature of the function isint f(int a[ ], int len) where len is the number of elements in 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{} -12. Write a function that takes an array of integers as an argument and returns a value based on thesums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array andlet Y = the sum of the even numbers. The function should return X - YIf you are using Java or C#, the signature of the function is:int f(int[ ] a)If you are using C or C++, the signature of the function is:int f(int[ ] a, int len) where len is the number of elements in a.1|Page
  • Examplesif 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{} 03. Write a function that accepts a character array, a zero-based start position and a length. It shouldreturn a character array containing containing length characters starting with the start character ofthe input array. The function should do error checking on the start position and the length and returnnull if the either value is not legal.If you are programming in Java or C#, the function signature is:char[ ] f(char[ ] a, int start, int len)If you are programming in C or C++, the function signature is:char * f(char a[ ], int start, int len, int lenA) where lenA is the number of elements in a.Examplesif 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}2|Page
  • {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 AnswersFirst 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[]{}); }3|Page
  • 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 answerpublic static void main(){ a2(new int[] {1});4|Page
  • 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;}5|Page
  • Third answerpublic 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) {6|Page
  • return null; } char[] sub = new char[length]; for (int i=start, j=0; j<length; i++, j++) { sub[j] = a[i]; } return sub;}7|Page