9 views

Original Title: Midterm Ans

Uploaded by Simon Tsoi

- AP Computer Science AB FRQ
- Sample 1 Questions Answer for the MUM Entrance Exam After Applying in the Maharishi University of Management
- Computer Fundamentals and Programming in c by Reema Thareja
- Amdocs-Campus Reg Form
- 2
- Intruders
- Amdocs Past Sample Test Papers
- Msp430 Manual
- Integrated M Tech Biotech
- Javascript
- Jensen K., Wirth N. PASCAL User Manual and Report.pdf
- C Programming MCQs
- Java Record
- C Programming Nov 2012
- Haskell Quick Guide
- test tl 7 answer key
- Adobe Placement Paper
- ap-computer-science-a-2014-java-quick-reference.pdf
- spec_test_1_for_BB
- c++ Answers Temp

You are on page 1of 8

Page 1 of 8

Semester II 20112012 Midterm Test

Suggested Solutions

1. Choose the correct answers (48 marks, 4 marks each) (1) Which of the following binary numbers is equal to 563? (A) 1001110111 (B) 1001110011 (C) 1000110011 (D) 1100110001

Ans: C

(2) What will be printed by the following program statement? System.out.println( 9/4 + 3/4 ); (A) 2 (B) 3.0 (C) 3 (D) 20

Ans: A

(3) Given that i is an int variable; c is a char variable; f is a float variable. What is the data type for the result for the following expression? f*(3.14*i+c) (A) char (B) int (C) float (D) double

Ans: D

(4) What will be the values of variables i and d after executing the following program statements? int i; double d=2.0; i=(int)(d*2.2); (A) i=4, d=2.0 (B) i=4.4, d=2.0 (C) i=4.4, d=4.4 (D) i=4, d=4.4

Ans: A

Page 2 of 8

(5) What will be the values of variables a, b and x after executing the following program statements? int a,b,x; a=b=x=1; b = ++a; x = ++a - b--; (A) a=2, b=2, x=2 (B) a=3, b=2, x=2 (C) a=3, b=1, x=2 (D) a=3, b=1, x=1

Ans: D

(6) What will be the values of variables x, a and b after executing the following program statements? boolean x, a=false, b=false, c=true; x = (a != (b = c)); (A) x=true, a=true, b=false (C) x=false, a=true, b=true (B) x=false, a=false, b=false (D) x=true, a=false, b=true

Ans: D

(7) What will be the value of variable x after executing the following program statements? int x=0, y=12; for (int i=0; i<=y; i+=4) x++; (A) x=2 (B) x=3 (C) x=4 (D) x=5

Ans: C

(8) What will be the value of variable x after executing the following program statements? int x, a=1; for (x=1; x<9; x++) { a++; if (a == 7) break; } (A) x=3 (B) x=4 (C) x=5 (D) x=6

Ans: D

Page 3 of 8

(9) What will be the value of variable x after executing the following program statements? int x, y, a=1; Stop: for (x=1; x<9; x++) for (y=1; y<3; y++) { a++; if (a == 7) break Stop; } (A) x=3 (B) x=4 (C) x=5 (D) x=6

Ans: A

(10) What is the output when the following method is executed? public static void M() { boolean b = true; if (b == false) { System.out.print("A"); } else if (b) { System.out.print("B"); } else { System.out.print("C"); } } (A) A (B) B (C) C (D) none of the above

Ans: B

(11) What will be printed by executing the program below? public class Call { public static void main(String [] args) { int x=1; x = mod4(x); x = mod4(x); x = mod4(x); System.out.println(x); } public static int mod4(int x) { return (x+1)%4; } } (A) 0 (B) 1 (C) 2 (D) 3

Ans: A

Page 4 of 8

(12) What is the output when the following method is executed? public static void M() { for (int i=1; i <= 6; i++) { for (int j=1; j <= 6; j++) { if ( i== 1 || i == 6) System.out.print("*"); else if ( j == 7-i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } (A) ****** * * * * ****** (B) ****** * * * * ****** (C) ****** * * * * * * * * ****** (D) ****** * * ** ** * * ******

Ans: B

2. Please provide the correct answers (24 marks, 6 marks each) (1) If x stores 5, and y stores 5, what will be printed after executing the following program statements? if (x>=2) if (y<=4) System.out.println(x); else System.out.println(y); System.out.println(x+y);

Ans: 5 10

Page 5 of 8

(2) What will be printed by executing the program below? public class A { public static void main(String [] args) { int size = 4; for (int i=1; i <= size; i++) { for (int j=1; j <= size-i; j++) System.out.print(" "); for (int j=1; j <= i; j++) System.out.print("+"); System.out.println(); } for (int i=1; i <= size-1; i++) { for (int j=1; j <= i; j++) System.out.print(" "); for (int j=1; j <= size-i; j++) System.out.print("+"); System.out.println(); } } }

Page 6 of 8

(3) What will be printed by executing the program below? public class A { public static void main(String [] args) { int num=5; for (int i=1; i<=num; i++) { for (int j=1; j<=(num-i); j++) System.out.print(" "); for (int k=1; k<=i; k++) System.out.print(k); System.out.println(); } } }

(4) What will be printed by executing the program below? public class A { public static void main(String [] args) { int x = 3, y = 5; xMethod(x, y); System.out.println("Output: " + (x + y)); } public static void xMethod(int x, int y) { x += y; } }

Ans: Output: 8

Page 7 of 8

3. (8 marks) Write a method sumSeries() to compute the summation of the following series:

1 + 2

2 + + 3

i i+1

The method takes an integer parameter num and returns the summation of the series m(num).

Ans: public static double sumSeries(int num) { double sum = 0; for ( int i=1; i<=num; i++) sum += Math.sqrt(i/(i+1.0)); return sum; }

4. (8 marks) Write a recursive method findLargestDigit() whose signature is given below: public static int findLargestDigit(int num)

The method accepts an integer parameter num and returns the the digit which is the largest one of the integer. For example, the method call findLargestDigit(245804) returns 8. Please complete the method findLargestDigit().

Ans: public static int findLargestDigit(int num) { if (num/10 == 0) return num; else return num%10 > findLargestDigit(num/10) ? num%10 : findLargestDigit(num/10); }

Page 8 of 8

5. (12 marks) Write a program to nd an integer between 1 and 10000 which has the largest number of divisors. When the integer is found, the program prints out the integer and all its divisors. It is possible that several integers in this range have the same maximum number of divisors. Your program has to print out the smallest one among them.

Ans: public class A { public static void main(String [] args) { int maxNum = 1; int maxInt = 1; for (int i = 1; i <= 10000; i++) { int count = 0; for (int j = 1; j <=i; j++) { if (i%j == 0) count ++; } if (count > maxNum) { maxNum = count; maxInt = i; } } System.out.println("The integer is " + maxInt); System.out.print("All divisors are: "); for (int i = 1; i<= maxInt; i++) if (maxInt%i == 0) System.out.print(i + " "); System.out.println("\n"); } }

-End-

- AP Computer Science AB FRQUploaded byzeustam
- Sample 1 Questions Answer for the MUM Entrance Exam After Applying in the Maharishi University of ManagementUploaded byMehmood Sultan
- Computer Fundamentals and Programming in c by Reema TharejaUploaded byShaik Yusaf
- Amdocs-Campus Reg FormUploaded bykannan_ei1084
- 2Uploaded byswh601
- IntrudersUploaded byAfzaal Mohammad
- Amdocs Past Sample Test PapersUploaded bymetrotrain
- Msp430 ManualUploaded bySilambarasan Madhaiyan
- Integrated M Tech BiotechUploaded byaib_himani
- JavascriptUploaded byGood Divya
- Jensen K., Wirth N. PASCAL User Manual and Report.pdfUploaded byneriovf
- C Programming MCQsUploaded byMaham Tanveer
- Java RecordUploaded bySuresh Prabhu
- C Programming Nov 2012Uploaded byharisummath5195
- Haskell Quick GuideUploaded byMurat Tanır
- test tl 7 answer keyUploaded byapi-236387090
- Adobe Placement PaperUploaded byGKGuide
- ap-computer-science-a-2014-java-quick-reference.pdfUploaded byRichard Krajichek
- spec_test_1_for_BBUploaded byKieren Shepherd
- c++ Answers TempUploaded byprabinseth
- Lec1Uploaded byJoon Suk Huh
- SPA Course Outcome n Lab Plan(Edited)Uploaded byVigneshRamakrishnan
- i8017hw User Manual Beta2Uploaded byPedro Bortot
- Lab 1Uploaded byBryan Wen Yao
- Home Game 1Uploaded byFrieda Ngaharjo
- Quiz Array Multple Choice With AUploaded byJeevan Balaka
- final reportUploaded byapi-241273053
- StructuresUploaded byKenKdw
- damely20Uploaded byTroy Briggs
- c for dspUploaded byjavsjavs2789

- 0521519004ArtificialIntelligenceUploaded byFernando Campos Cano
- Survival Models Solution Chapter 2Uploaded byNayaz Maudarbucus
- Craig Kaplan - Computer Generated Islamic Star PatternsUploaded byNasia Aisan
- Vectors TestUploaded byAbraham Robinson
- Lecture 40Uploaded bynp_nikhil
- 1.8 Piecewise Functions Note & HwUploaded bykcykim4
- Medieval Arithmetic GeometryUploaded byDaniel G.G.
- Common Core Math Grade 4 Fraction Addition SubtractionUploaded byaashima24
- DIP3E Errata SheetUploaded bySteven J Lee
- math paper 9709Uploaded bysriniyfa
- TaylorUploaded byTen Mukhi
- Solution of Boundary-Value Problems by Orthogonal CollocationUploaded byThirumalaimuthukumaranMohan
- ruswpob.pdfUploaded byAnonymous MqprQvjEK
- Foundation Unit 8 Topic TestUploaded byLouis Sharrock
- 5_3_Parallel Intersecting or Skew LinesUploaded bySudibyo Gunawan
- IMC2016 Sol ShortUploaded byDoddy Feryanto
- MomentsUploaded byJohn Kristoffer Gimpes
- ReportUploaded bySudipta Karmakar
- Indian Mathematics HistoryUploaded byapi-3719538
- Project RaduUploaded byAlexa Strumf
- Finite Element Method (2)Uploaded byVivek Patel
- Cs at SamplesUploaded bydwivedi89
- 2 Matematika Teknik 2018Uploaded byMagisterTS
- national5lifeskillsnotesjedburghgrammarUploaded byapi-163891747
- Cultural Bias Investigation in Trigonometry: Sixth Edition, written by Charles P. McKeague and Mark D. TurnerUploaded byEducational Materials
- Important Instructions During PreparationUploaded byRahulsinghoooo
- ethz_lecture6Uploaded byprasaad08
- School Geometry III IV HallUploaded byKirti Chhaya
- Hierarchical Genetic Algorithms for the Derivation of L-SystemUploaded bykamranali
- Krylov Subspace Methods for Solving Large Unsymmetric Linear Systems [Saad]Uploaded byyanivscribd