COS 126 Fall 2006 Exam 2 Solutions and Discussions, Exams of Computer Science

The solutions and discussions for exam 2 of the cos 126 fall 2006 course. It includes java code snippets, explanations of algorithms, and discussions on the limitations of program verification.

Typology: Exams

2011/2012

Uploaded on 07/16/2012

sanjay
sanjay 🇮🇳

5

(2)

26 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COS 126 Fall 2006 Exam 2 Solutions
1. Reading down the list: G, C, A, B, E, F
2.
public class Palindrome {
public static boolean isPunc(char c){
if ((c==' ')|| (c==',')||(c =='.')||(c==':')||
(c==';')||(c=='!')||(c=='?')) return true;
else return false;
}
public static void main(String[] args) {
String s = args[0].toLowerCase();
boolean pal = true;
for (int i=0, j=s.length()-1; i < j; i++, j--) {
while (isPunc(s.charAt(i))) i++;
while (isPunc(s.charAt(j))) j--;
if (s.charAt(i) != s.charAt(j)) pal = false;
}
System.out.println(pal);
}
}
docsity.com
pf3
pf4

Partial preview of the text

Download COS 126 Fall 2006 Exam 2 Solutions and Discussions and more Exams Computer Science in PDF only on Docsity!

COS 126 Fall 2006 Exam 2 Solutions

  1. Reading down the list: G, C, A, B, E, F

public class Palindrome { public static boolean isPunc(char c){ if ((c==' ')|| (c==',')||(c =='.')||(c==':')|| (c==';')||(c=='!')||(c=='?')) return true; else return false; } public static void main(String[] args) { String s = args[0].toLowerCase(); boolean pal = true; for (int i=0, j=s.length()-1; i < j; i++, j--) { while (isPunc(s.charAt(i))) i++; while (isPunc(s.charAt(j))) j--; if (s.charAt(i) != s.charAt(j)) pal = false; } System.out.println(pal); } }

  1. Let the bits be named b8, b7,... , b
  1. Reading down the list: T, T, F, F, T, F, T, F, T, T

public class Client { public static void main (String[] args) { int N = Integer.parseInt(args[0]); Interval[] intervals = new Interval[N]; for (int i = 0; i < N; i++) { intervals[i] = new Interval(StdIn.readDouble(), StdIn.readDouble()); } for (int i = 0; i < N; i++){ for (int j = i + 1; j < N; j++) { if (intervals[i].intersects(intervals[j])) System.out.println(intervals[i] + " intersects " + intervals[j]); } } } }

  1. Had you taken COS 126 you would have learned that your idea for the Verifier is, sadly, impossible to realize, for it is not even possible to write a computer program that would say, correctly and always, whether an arbitrary other program would eventually halt on a given input. Your Verifier would have this impossible ability, and so cannot exist. Please don’t fire me.