Tag: programming languages

Questions Related to programming languages

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?

  1. With assertions enabled it prints 210210-1 followed by an AssertionError message.

  2. With assertions enabled it prints 210210 followed by an AssertionError message.

  3. With assertions enabled it prints only 210210

  4. With assertions enabled it prints nothing.

  5. With assertions disabled it prints 210210-1

  6. With assertions disabled it prints only 210210


Correct Option: B,E

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?

  1. With assertions enabled it prints ABC followed by an AssertionError message.

  2. With assertions disabled it prints ABC followed by an AssertionError message.

  3. Assertions should not be used within the default case of a switch statement.

  4. In this code example a throw statement must be used in place of the assert statement.

  5. Compile-time error


Correct Option: D,E

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?

  1. If assertions are not enabled at run time it prints an error message.

  2. If assertions are not enabled at run time it prints nothing.

  3. With assertions enabled it prints an error message.

  4. With assertions enabled it prints nothing.

  5. The assert statement is being used to check a class invariant--something that must be true about each instance of the class

  6. The assert statements are being used to check a precondition--something that must be true when the method is invoked.


Correct Option: B,D,E

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"; }

  1. The digit is 3

  2. The digit is 2

  3. The digit is 1

  4. None of the above


Correct Option: B

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; }

  1. text always precedes the

  2. text always precedes the end of the end

  3. text always precedes the end of the

  4. None of the above


Correct Option: C

What is the output of the following code snippet: $mystring = "[2009/02/14] The date of this article."; if($mystring =~ m/(\d+)/) { print "$1"; }

  1. 14

  2. 02

  3. 2009

  4. Valentine's Day


Correct Option: C

a\Sz

  1. would match any three-character string starting with "a" and ending with "z" whose second character was not a space, tab or newline.

  2. the second character could be a letter, number or symbol

  3. would match any three-character string starting with "a" and ending with "z" whose second character was a newline

  4. None of the above


Correct Option: A,B

int (*p)(char *a)

  1. 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

  2. p is a pointer to a function that accepts an argument which is a pointer to a character and returns an integer quantity

  3. p is a function that accepts an argument which is a pointer to a character and returns an integer quantity

  4. None of the above


Correct Option: B

int *p(void)

  1. p is a function that accepts an argument which is a pointer to a character and returns an integer quantity

  2. p is a function that accepts an argument which is a pointer to a character and returns a pointer to an integer quantity

  3. p is a function that returns a pointer to an integer quantity

  4. None of the above


Correct Option: C
  1. will match "cyclopentane"

  2. will match "tri-pentane"

  3. will match "n-pentane"

  4. will match "neopentane"


Correct Option: A,B,D