1 / 10

CS1101: Programming Methodology Recitation 8– Revision

CS1101: Programming Methodology Recitation 8– Revision. What of the value of mystery when the following code is executed ? String text, mystery; text = "mocha chai latte"; Mystery = text.substring(1, 5); 2. What is the value of text3 when the following code is executed ?

Download Presentation

CS1101: Programming Methodology Recitation 8– Revision

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1101: Programming MethodologyRecitation 8– Revision

  2. What of the value of mystery when the following code is executed ? String text, mystery; text = "mocha chai latte"; Mystery = text.substring(1, 5); 2. What is the value of text3 when the following code is executed ? String text1 = "a" + "b"; String text2 = "c"; String text3 = text1 + text2 + text1; • Determine the value of result. String text = "Java Programming"; String result = text.substring(5,6) + text.substring (text.length()-3,text.length());

  3. What is wrong with the following ? y = sqrt(38.0); y = Math.exp(2, 3); y = math.sqrt(b*b – 4*a*c) / (2*a); 5. Determine the output of the following code. int x, y; x = 1; y = 2; System.out.println ("The output is " + x + y); System.out.println ("The output is " + (x + y));

  4. 6. Consider the following instantiable class. class QuestionOne { public final int A = 345; public int b; private float c; private void methodOne (int a) { b = a; } public float methodTwo () { return 23; } } Identify invalid statements in the following main class. For each invalid statement, state why it is invalid. class Q1Main { public static void main (String[] args) { QuestionOne q1; q1 = new QuestionOne (); q1.A = 12; q1.b = 12; q1.c = 12; q1.methodOne(12); q1.methodOne(); System.out.println (q1.methodTwo(12)); q1.c = q1.methodTwo(); } }

  5. 7. What is the output of the following code ? class Q2Main { public static void main (String[] args) { QuestionTwo q2; q2 = new QuestionTwo (); q2.init(); q2.count = q2.increment() + q2.increment(); System.out.println (q2.increment()); } } class QuestionTwo { public int count; public void init () { count = 1; } public int increment () { count = count + 1; return count; } }

  6. 8. What is the value of sum after the following nested loops are executed ? • sum = 0; for (int i = 0; i <= 5; i++) { for (int j = 10; j > 2*i; j--) { sum = sum + (j – i); } } b. sum = 0; i = 0; while (i < 5) { j = 5; while (i != j) { sum += j; j--; } i++; }

  7. 9. What of the following statements are invalid ? • float number[23]; • float number = {1.0f, 2.0f, 3.0f}; • int number; number = new Array[23]; • int[] number = [1, 2, 3, 4]; 10. What is the output of the following code ? try { num = Integer.parseInt("a123"); if (num < 0) { throw new Exception("No negative"); } } catch (NumberFormatException e) { System.out.println ("Cannot convert to int"); } catch (Exception e) { System.out.println ("Error: " + e.getMessage()); } finally { System.out.println ("Done"); }

  8. Solutions • mystery = "ocha" • text3 = "abcab" • result = "Ping" 4. y = Math.sqrt(38.0); y = Math.exp(2, 3); //Math.exp expects a double parameter, not two int values y = Math.sqrt(b*b – 4*a*c) / (2*a); 5. The output is 12 The output is 3

  9. Solutions 6. class Q1Main { public static void main (String[] args) { QuestionOne q1; q1 = new QuestionOne (); q1.A = 12; //cannot assign a value to final variable A q1.b = 12; q1.c = 12; // c has private access in QuestionOne q1.methodOne(12); // methodOne(int) has private access in QuestionOne q1.methodOne(); // methodOne(int) in QuestionOne cannot be applied to () System.out.println (q1.methodTwo(12)); //methodTwo() in QuestionOne cannot be applied to (int) q1.c = q1.methodTwo (); //c has private access in QuestionOne } }

  10. Solutions • Output is 6 8a. Sum is 165 8b. Sum is 55 9a. float number[23]; //23 should not be there 9b. float[] number = {1.0f, 2.0f, 3.0f}; //[] is missing 9c. int number; number = new Array[23]; // incompatible types // found: Array[], required: int 9d. int[] number = [1, 2, 3, 4]; //{} instead of [] • Cannot convert to int Done.

More Related