CS1101: Programming Methodology Recitation 8 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1101: Programming Methodology Recitation 8

Description:

2. What is the value of text3 when the following code is executed ? String text1 ... mystery = 'ocha' text3 = 'abcab' result = 'Ping' 4. y = Math.sqrt(38.0) ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 11
Provided by: leemo4
Category:

less

Transcript and Presenter's Notes

Title: CS1101: Programming Methodology Recitation 8


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"

3
  • What is wrong with the following ?
  • y sqrt(38.0)
  • y Math.exp(2, 3)
  • y math.sqrt(bb 4ac) / (2a)
  • 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.methodO
ne() System.out.println (q1.methodTwo(12)) q
1.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


6
  • 8. What is the value of sum after the following
    nested loops are executed ?
  • sum 0
  • for (int i 0 i lt 5 i)
  • for (int j 10 j gt 2i j--)
  • sum sum (j i)
  • b. sum 0
  • i 0
  • while (i lt 5)
  • j 5
  • while (i ! j)
  • sum j
  • j--
  • i

7
  • 9. What of the following statements are invalid ?
  • float number23
  • float number 1.0f, 2.0f, 3.0f
  • int number
  • number new Array23
  • int number 1, 2, 3, 4
  • 10. What is the output of the following code ?
  • try
  • num Integer.parseInt("a123")
  • if (num lt 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())

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(bb 4ac) / (2a)
  • 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 ()

10
Solutions
  • Output is 6
  • 8a. Sum is 165
  • 8b. Sum is 55
  • 9a. float number23 //23 should not be there
  • 9b. float number 1.0f, 2.0f, 3.0f // is
    missing
  • 9c. int number
  • number new Array23 // incompatible types
  • // found Array, required int
  • 9d. int number 1, 2, 3, 4 // instead
    of
  • Cannot convert to int
  • Done.
Write a Comment
User Comments (0)
About PowerShow.com