100 likes | 205 Views
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 ?
E N D
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());
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));
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(); } }
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; } }
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++; }
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"); }
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
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 } }
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.