Programming Languages: Perl, Java, and C

Test your knowledge of Perl, Java, and C programming languages with questions covering syntax, control flow, pointers, exception handling, and OOP concepts.

20 Questions Published

Questions

Question 1 True/False

Encapsulation is the process of basing one class on another.

  1. True
  2. False
Question 2 Multiple Choice (Single Answer)

#include<stdio.h> main() { char a[4]="HELLO"; printf("%s",a); }

  1. O
  2. H
  3. HELLO
  4. Compilation Error
Question 3 Multiple Choice (Single Answer)

#include<stdio.h> main() { int a[2][2][2] = {{10,2,3,4},{5,6,7,8}}; int *p,*q; p=&a[2][2][2]; *q=***a printf("%d----%d",*p,*q); }

  1. Compilation error
  2. syntax error
  3. 10 2 3 4 5 6 7 8
  4. print the address of pointer p and q
Question 4 Multiple Choice (Single Answer)

#include<stdio.h> main (){ unsigned int a = 0xf0ad; char *c = (char *) &a; printf("value @c: %x",*c); }

  1. value @c: ffffffad
  2. value @c: 0xf0ad
  3. compilation error
  4. syntax error
Question 5 Multiple Choice (Single Answer)

#include<stdio.h> main(){ extern int i; i=23; printf("%d",i); }

  1. 23
  2. Prints some address
  3. Compilation error
  4. Run time error
Question 6 Multiple Choice (Single Answer)

#include<stdio.h> main(){ static int a[20]; int i=0; a[i]=i++; printf("%d %d %d",a[0],a[1],i); }

  1. 0 0 0
  2. 0 1 0
  3. 0 0 1
  4. 0 1 1
Question 7 Multiple Choice (Single Answer)

#include<stdio.h> main(){ int a=2; int *p=&a; print("%x\t",p); print("%x\t",*p++); print("%x\t",p); }

  1. someaddress value someaddress
  2. value value value
  3. syntax error
  4. compilation error
Question 8 Multiple Choice (Single Answer)

main() { int a[5] = {10,20,30,40,50}; char *p; p=(char *)a; printf("%d\n",a); }

  1. some value
  2. compilation error
  3. 10
  4. 50
Question 9 Multiple Choice (Single Answer)

#include<stdio.h> main (){ char c[] = "Hello"; char *p = "Hello"; printf("Size of c: %d Addr of c:%x\n",sizeof(c),c); }

  1. Size of c: some value Addr of c:some address
  2. Size of c: some value Addr of c:some value
  3. Size of c: some address Addr of c:some address
  4. Compilation error
Question 10 True/False

Can a variable be declared as both const and volatile

  1. True
  2. False
Question 11 Multiple Choice (Single Answer)

Which of the below symbol is used to make comments in C language?

  1. //
  2. #
  3. !!
  4. <!--
Question 12 Multiple Choice (Single Answer)

public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main(String[] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } }

  1. 57 22
  2. 45 38
  3. 45 57
  4. D. An exception occurs at runtime
Question 13 Multiple Choice (Single Answer)

class Plane { static String s = "-"; public static void main(String[] args) { new Plane().s1(); System.out.println(s); } void s1() { try { s2(); } catch (Exception e) { s += "c"; } } void s2() throws Exception { s3(); s += "2"; s3(); s += "2b"; } void s3() throws Exception { throw new Exception(); } }

  1. -
  2. -c
  3. -c2
  4. -2c
  5. -c22b
  6. -2c2b
Question 14 Multiple Choice (Multiple Answers)
  1. class Loopy { 2. public static void main(String[] args) { 3. int[] x = {7,6,5,4,3,2,1}; 4. // insert code here 5. System.out.print(y + " "); 6. } 7. } }
  1. for(int y : x) {
  2. for(x : int y) {
  3. int y = 0; for(y : x) {
  4. for(int y=0, z=0; z<x.length; z++) { y = x[z];
  5. for(int y=0, int z=0; z<x.length; z++) { y = x[z];
  6. int y = 0; for(int z=0; z<x.length; z++) { y = x[z];
Question 15 Multiple Choice (Single Answer)

Given: 3. public class Circles { 4. public static void main(String[] args) { 5. int[] ia = {1,3,5,7,9}; 6. for(int x : ia) { 7. for(int j = 0; j < 3; j++) { 8. if(x > 4 && x < 8) continue; 9. System.out.print(" " + x); 10. if(j == 1) break; 11. continue; 12. } 13. continue; 14. } 15. } 16. }

  1. 1 3 9
  2. 5 5 7 7
  3. 1 3 3 9 9
  4. 1 1 3 3 9 9
  5. 1 1 1 3 3 3 9 9 9
  6. Compilation fails
Question 16 Multiple Choice (Single Answer)
  1. public class Wind { 4. public static void main(String[] args) { 5. foreach: 6. for(int j=0; j<5; j++) { 7. for(int k=0; k< 3; k++) { 8. System.out.print(" " + j); 9. if(j==3 && k==1) break foreach; 10. if(j==0 || j==2) break; 11. } 12. } 13. } 14. }
  1. 0 1 2 3
  2. 1 1 1 3 3
  3. 0 1 1 1 2 3 3
  4. 1 1 1 3 3 4 4 4
  5. 0 1 1 1 2 3 3 4 4 4
  6. compilation fails
Question 17 Multiple Choice (Single Answer)

What will be printed by the code below? my $val = 'x'; print ref($val);

  1. SCALAR
  2. empty value
  3. STRING
  4. "not a reference"
Question 18 Multiple Choice (Single Answer)

What will be printed by the following code? package A; sub new { return bless {}; } sub foo { return 'A'; } package B; use base 'A'; sub foo { return 'B'; } package main; my $obj = B->new(); print $obj->foo(), "\n";

  1. A
  2. B
  3. the code will fail
  4. None of the above
Question 19 Multiple Choice (Single Answer)

What will be the value of $val? my $str = 'aa bb cccc'; my $val = () = $str =~ /\w+/g;

  1. undef
  2. aa
  3. cccc
  4. 3
  5. 0
Question 20 Multiple Choice (Single Answer)

What gets printed? my $a = 123; my $b = 0123; if ($a == $b) { print "same"; } else { print "different"; }

  1. same
  2. different
  3. the code is ill-formed
  4. None of the above