220 likes | 537 Views
Week 10 – Quiz!!. 1 st Round. Spot the bug! Answers : 1 – Loop never finishes 2 – Won’t repeat while loop 3 – Infinite loop 4 – Array out of bounds exception. Spot the bug -1 . boolean santa=true; while(santa=true) santa=false; ->Loop never finishes. Spot the bug - 2.
E N D
1st Round Spot the bug! Answers : 1 – Loop never finishes 2 – Won’t repeat while loop 3 – Infinite loop 4 – Array out of bounds exception
Spot the bug -1 boolean santa=true; while(santa=true) santa=false; ->Loop never finishes
Spot the bug - 2 String reindeer; BufferedReader in = new BufferedReader(System.in); do { System.out.print("Any more?”); reindeer=in.readLine(); } while(reindeer=="y"); -> Won’t repeat while loop
Spot the bug - 3 int i = 0; while (i < 10) System.out.println(i); i++; System.out.println(“Finished”); -> Infinite loop
Spot the bug - 4 String astrNames[]= {"Dasher","Donner","Blitzen"}; For (int i=1;i<=astrNames.length;i++){ System.out.println(astrNames[i]); } -> Array out of bounds exception
2nd Round Match the Error! • array dimension missing • java.lang.NullPointerException • <identifier> expected • ArrayOutOfBoundsExeption • illegal start of expression • variable s might not have been initialized • '(' or '[' expected
Match the Error - 1 public class Sleigh { public static void main (String args[]) { String s = null; System.out.println(s.length()); } }
Match the Error - 2 public class Sleigh { public static void main (String args[]) { String s; System.out.println(s.length()); } }
Match the Error - 3 public class Sleigh { public static void main (String args[]) { String s = null; for (int i = 1; i< 10; i++) s+=i; } System.out.println(s.length()); } }
Match the Error - 4 public class Sleigh { public static void main (String args[]) { String s = new String; for (int i = 1; i< 10; i++) s+=i; System.out.println(s.length()); } }
Match the Error - 5 public class Sleigh { public static void main (String args[]) { String s[] = new String[]; for (int i = 1; i< 10; i++) s[i]+=i; System.out.println(s); } }
Match the Error - 6 public class Sleigh { public static void main (String args[]) { String s[] = new String({"Dasher","Donner","Blitzen"}); for (int i = 1; i< 10; i++) s[i]+=i; System.out.println(s.length()); } }
Match the Error - 7 public class Sleigh { public static void main (String args[]) { String s[] = new String[10]; for (int i = 1; i <= s.length; i++) s[i]=i+""; System.out.println(s[0]); } }
Match the Error - Answers • 1 – B • 2 – F • 3 – C • 4 – G • 5 – A • 6 – E • 7 - D
Match the Error - 1 public class Sleigh { public static void main (String args[]) { String s = null; System.out.println(s.length()); } } Exception in thread "main" java.lang.NullPointerException at Sleigh.main(Sleigh.java:5)
Match the Error - 2 public class Sleigh { public static void main (String args[]) { String s; System.out.println(s.length()); } } Sleigh.java:5: variable s might not have been initialized System.out.println(s.length()); ^ 1 error
Match the Error - 3 public class Sleigh { public static void main (String args[]) { String s = null; for (int i = 1; i< 10; i++) s+=i; } System.out.println(s.length()); } } Sleigh.java:8: <identifier> expected System.out.println(s.length()); ^
Match the Error - 4 public class Sleigh { public static void main (String args[]) { String s = new String; for (int i = 1; i< 10; i++) s+=i; System.out.println(s.length()); } } Sleigh.java:4: '(' or '[' expected String s = new String; ^ 1 error
Match the Error - 5 public class Sleigh { public static void main (String args[]) { String s[] = new String[]; for (int i = 1; i< 10; i++) s[i]+=i; System.out.println(s); } } Sleigh.java:4: array dimension missing String s[] = new String[]; ^ 1 error
Match the Error - 6 public class Sleigh { public static void main (String args[]) { String s[] = new String({"Dasher","Donner","Blitzen"}); for (int i = 1; i< 10; i++) s[i]+=i; System.out.println(s.length()); } } Sleigh.java:4: illegal start of expression String s[] = new String({"Dasher","Donner","Blitzen"}); ^ Sleigh.java:4: ')' expected String s[] = new String({"Dasher","Donner","Blitzen"}); ^ 2 errors
Match the Error - 7 public class Sleigh { public static void main (String args[]) { String s[] = new String[10]; for (int i = 1; i <= s.length; i++) s[i]=i+""; System.out.println(s[0]); } } Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at Sleigh.main(Sleigh.java:6)