Programming Languages: C, Java, Perl Features
Test your knowledge of programming language features including C pointers, Java assertions and encapsulation, and Perl regular expressions.
Questions
Which of these lists contains at least one word that is not a Java keyword?
- abstract, default, if, private, this
- do, implements, protected, boolean, throw
- case, extends, int, short, try
- import, break, double, exception, throws
- byte, else, instanceof, return, transient
- None of the above
class Identifiers { int i1; // 1 int _i2; // 2 int i_3; // 3 int #i4; // 4 int $i5; // 5 int %i6; // 6 int i$7; // 7 } Compile-time errors are generated at which lines?
- 1
- 2
- 3
- 4
- 5
- 6
Which of the following are true statements?
- Encapsulation is a form of data hiding.
- A tightly encapsulated class is always immutable.
- Encapsulation is always used to make programs run faster.
- Encapsulation helps to protect data from corruption.
- Encapsulation allows for changes to the internal design of a class while the public interface remains unchanged.
Which of the following are true statements?
- A top-level class can not be called "tightly encapsulated" unless it is declared private.
- Encapsulation enhances the maintainability of the code.
- A tightly encapsulated class allows fast public access to member fields.
- A tightly encapsulated class allows access to data only through accessor and mutator methods.
- Encapsulation usually reduces the size of the code.
- A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model.
A class can not be called "tightly encapsulated" unless which of the following is true?
- The class is declared final.
- All local variables are declared private.
- All method parameters are declared final.
- No method returns a reference to any object that is referenced by an internal data member.
- None of the above
class GFC500 {private String name;} class GFC501 { private String name; private void setName(String name) {this.name = name;} private String getName() {return name;} } class GFC502 { private String name; public void setName(String name) {this.name = name;} public String getName() {return name;} } Which class is not tightly encapsulated?
- GFC501
- GFC502
- GFC503
- None of the above
class GFC506 {private String name;} class GFC507 extends GFC506 { String name; public void setName(String name) {this.name = name;} public String getName() {return name;} } class GFC508 extends GFC506 { private String name; public GFC508(String name) {setName(name);} public void setName(String name) {this.name = name;} public String getName() {return name;} } Which class is not tightly encapsulated?
- GFC506
- GFC507
- GFC508
- None of the above
Which of the following are command-line switches used to enable assertions in non-system classes?
- -ea
- -ae
- -aon
- -assertionsenable
- -enableassertions
Which of the following are command-line switches used to disable assertions in non-system classes?
- -disableassertions
- -disableAssertions
- -assertionsDisable
- -assertionsOn
- -assertionsOff
class A { void m1(int i) { int j = i % 3; switch (j) { case 0: System.out.print("0"); break; case 1: System.out.print("1"); break; default: assert j == 2; System.out.print(j); }} public static void main (String[] args) { A a = new A(); for (int i=5; i >= -1; i--) {a.m1(i);} }} Which statements are true?
- With assertions enabled it prints 210210-1 followed by an AssertionError message.
- With assertions enabled it prints 210210 followed by an AssertionError message.
- With assertions enabled it prints only 210210
- With assertions enabled it prints nothing.
- With assertions disabled it prints 210210-1
- With assertions disabled it prints only 210210
class C { String m1(int i) { switch (i) { case 0: return "A"; case 1: return "B"; case 2: return "C"; default: assert false; } } public static void main(String[] args) { C c = new C(); for (int i = 0; i < 4; i++) { System.out.print(c.m1(i)); }}} Which statements are true?
- With assertions enabled it prints ABC followed by an AssertionError message.
- With assertions disabled it prints ABC followed by an AssertionError message.
- Assertions should not be used within the default case of a switch statement.
- In this code example a throw statement must be used in place of the assert statement.
- Compile-time error
class C { int a, b, c; public void setA(int i) {a = i; assert validateC() : c;} public void setB(int i) {b = i; assert validateC() : c;} private boolean validateC() { return c > a + 2 * b; } public int m1(int i) { c = a + b + i; assert validateC() : c; return c; } public C(int i) { c = i; assert validateC() : c; } public static void main(String[] args) { C c = new C(251); c.setA(50); c.setB(100); }} Which statements are true?
- If assertions are not enabled at run time it prints an error message.
- If assertions are not enabled at run time it prints nothing.
- With assertions enabled it prints an error message.
- With assertions enabled it prints nothing.
- The assert statement is being used to check a class invariant--something that must be true about each instance of the class
- The assert statements are being used to check a precondition--something that must be true when the method is invoked.
What is the ouput of the following code snippet: $mystring = "[2004/04/13] The date of this article."; if($mystring =~ m/(\d)/) { print "The digit is $1"; }
- The digit is 3
- The digit is 2
- The digit is 1
- None of the above
What is the output of the follwing code snippet: $mystring = "The start text always precedes the end of the end text."; if($mystring =~ m/start(.*)end/) { print $1; }
- text always precedes the
- text always precedes the end of the end
- text always precedes the end of the
- None of the above
What is the output of the following code snippet: $mystring = "[2009/02/14] The date of this article."; if($mystring =~ m/(\d+)/) { print "$1"; }
- 14
- 02
- 2009
- Valentine's Day
a\Sz
- would match any three-character string starting with "a" and ending with "z" whose second character was not a space, tab or newline.
- the second character could be a letter, number or symbol
- would match any three-character string starting with "a" and ending with "z" whose second character was a newline
- None of the above
int (*p)(char *a)
- p is a function that accepts an argument which is a pointer to a character and returns a pointer to a 10 element integer array
- p is a pointer to a function that accepts an argument which is a pointer to a character and returns an integer quantity
- p is a function that accepts an argument which is a pointer to a character and returns an integer quantity
- None of the above
int *p(void)
- p is a function that accepts an argument which is a pointer to a character and returns an integer quantity
- p is a function that accepts an argument which is a pointer to a character and returns a pointer to an integer quantity
- p is a function that returns a pointer to an integer quantity
- None of the above
int p(char *a[])
- p is a pointer to a function that accepts an argument which is a pointer to a character and returns an integer quantity
- p is a function that accepts an argument which is a pointer to a character array and returns an integer quantity
- p is a function that accepts an argument which is a pointer to a character and returns a pointer to a 10 element integer array
- None of the above