Document 10805477

advertisement
Short Quiz 11 1) What will happen when you run this program ? vector<int> v(10); for (int i = 0; i<v.size(); ++i) v[i] = i; // set values for (int i = 0; i<=15; ++i) cout << "v[" << i << "] == " << v[i] << endl; Ans: throwing a Range_error 2) Briefly explain pre-­‐condition and post-­‐condition. Ans: • Check if functions’ arguments are all evaluated as true, and such a requirement is called a pre-­‐condition • Check if the return value evaluated as valid, and such a requirement is called a post-­‐condition. 3) Please write down result of following program: public class TestClass{ public static void main (String[]args) { int a = new TestClass().absorbeTheValue(); } int absorbeTheValue(){ try{ int a = 10/0; if (a > 0) return 4; } catch(Exception e){ return 45; } finally{ System.out.println("this is finally"); return 34; } } } Ans: this is finally 4) Please write down result of following program: public class TestClass{ public static void main (String[]args) { int a = new TestClass().absorbeTheValue(); } int absorbeTheValue(){ try{ int a = 10/0; if (a > 0) return 4; } catch(Exception e){ return 45; } finally{ try{ System.out.println("this is try-­‐catch inside finally"); } catch( Exception e ){} System.out.println("this is finally"); Ans: this is try-­‐catch inside finally return 34; this is finally } } } 5) Please write down result of following program: public class TestClass{ public static void main (String[]args) { int a = new TestClass().absorbeTheValue(); } int absorbeTheValue(){ try{ Ans: Syntax Error int a = 10/0; if (a > 0) return 4; } catch(Exception e){ return 45; } finally{ return 18; try{ System.out.println("this is try-­‐catch inside finally"); } catch( Exception e ){} System.out.println("this is finally"); return 34; } } } 6) Please write down result of following program: public class TC{ public static void main (String[]args) { int a = new TC().absorbeTheValue(); Ans: this is the first time exception } this is try-­‐catch inside finally int absorbeTheValue(){ this is finally try{ int a = 10/0; if (a > 0) return 4; } catch( ArithmeticException ae) { System.out.println("this is the first time exception"); throw new Exception(); } catch(Exception e){ System.out.println("this is the second time catching exception"); throw new ArithmeticException(); } finally{ try{ System.out.println("this is try-­‐catch inside finally"); } catch( Exception e ){} System.out.println("this is finally"); return 34; } } } 7) Please draw stack trace of following java program: public class Retry { static int i = 0; public void f() { try { g(); } catch(Exception e) { System.out.println("Caught exception, i = " + i); i++; f(); } } void g() throws gException { if (i < 3) { throw new gException(); } else System.out.println("g() is working now"); } !"##$%&"'()$*+&,$-.'/0123$
Ans: main() called
f() called
main()
f()
main()
g() called
search f() for
handler, exit f()
g()
f()
main()
f()
main()
exception thrown,
g() has no handler,
exit g()
main()
search main() for
handler, call
printStackTrace(),
exit main()
8) Briefly explain resumption and termination in Java. Ans: • Java makes it hard to complete this cycle: – find a problem, – throw an exception, – fix the problem in the handler, and – go back to where you left off. • This is called “resumption”. • Java assumes you don’t want to go back. • This is called “termination”. 9) Please answer the result of following python program: try: fin = open('bad_file') Ans: Traceback (most recent call for line in fin: last): print line File "ce.py", line 2, in <module> fin.close() fin = open('bad_file') except KeyError: IOError: [Errno 2] No such file or print 'Something went wrong.' directory: 'bad_file' 10) What is the result of following program if we input height as “6 feet” and weight as “100 lbs”? if (isset($_GET['height']) && isset($_GET['weight'])) { if (!is_numeric($_GET['weight']) || !is_numeric($_GET['height'])) { trigger_error(“User did not enter numeric values”, E_USER_ERROR); exit(); } } else trigger_error(“User did not enter values”, E_USER_ERROR); $BodyMass = $_GET['weight'] / ($_GET['height'] * $_GET['height']) * 703; printf(“<p>Your body mass index is %d.</p>”, $BodyMass); Ans: User did not enter numeric values 
Download