Java and Perl Programming Fundamentals

Test your knowledge of Java and Perl programming languages with questions covering code output, syntax, OOP concepts, threading, error handling, and language-specific features.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

How many key-value pairs will the hash contain? my %hash = ( [1, 2] => 1, [1, 2] => 2 );

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

What gets printed? package A; sub new { my $class = shift; my $self = {}; $self->init(); return bless $self, $class; }; sub init { my $self = shift; $self->{key} = 'value'; } sub get { my ($self, $key) = @_; return $self->{$key}; } package main; my $obj = A->new(); print $obj->get('key'), "\n";

  1. empty string
  2. value
  3. the code will fail
  4. None of the above
Question 3 Multiple Choice (Single Answer)

: What will be the size of the @fields array? my $record = ':a:b:c:'; my @fields = split(':', $record, -1);

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

What gets printed? my @a = ([1, 2, 3, 4], [5, 6, 7, 8]); print join(' ', @{$a[1]}[1..3]);

  1. 1 2 3
  2. 2 3 4
  3. 5 6 7
  4. 6 7 8
Question 5 Multiple Choice (Single Answer)

What will be printed?my @a = (1, undef, 2);my $sum = 0;foreach my $val (@a) { eval { $sum += foo($val); }; if ($@) { $sum += 100; }}print "$sum\n";sub foo { my $val = shift; die "I don't like undefs" unless defined $val; return $val + $val;}

  1. 2
  2. 6
  3. 106
  4. I don't like undefs
Question 6 Multiple Choice (Single Answer)

What will be the value of $keys after execution of the following code? my $var; if (exists $var->{key1}->{key2}) { $var->{key1}->{key2} = 1; } my $keys = keys(%{$var});

  1. undef
  2. 0
  3. 1
  4. 2
  5. code will fail
Question 7 Multiple Choice (Single Answer)

What will be printed? BEGIN { print 'a'; } END { print 'b'; } BEGIN { print 'c'; } END { print 'd'; }

  1. abcd
  2. acbd
  3. cadb
  4. code will fail
Question 8 Multiple Choice (Single Answer)

What will be printed by the code below? my @a = (0, 1, 2); my ($b) = @a; print $b;

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

What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName()); } } public class Test { public static void main(String... args) { A a = new A(); Thread t = new Thread(a); Thread t1 = new Thread(a); t.setName("t"); t1.setName("t1"); t.setPriority(10); t1.setPriority(-3); t.start(); t1.start(); } }

  1. t t1
  2. t1 t
  3. t t
  4. Compilation succeed but Runtime Exception
Question 10 Multiple Choice (Single Answer)

What is the output for the below code ? class A implements Runnable{ public void run(){ System.out.println(Thread.currentThread().getName()); } } 1. public class Test { 2. public static void main(String... args) { 3. A a = new A(); 4. Thread t = new Thread(a); 5. t.setName("good"); 6. t.start(); 7. } 8. }

  1. good
  2. null
  3. Compilation fails with an error at line 5
  4. Compilation succeed but Runtime Exception
Question 11 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test { public static void main(String[] args) { List list = new ArrayList(); list.add(0, 59); int total = list.get(0); System.out.println(total); } }

  1. 59
  2. Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();
  3. Compile time error, because can't add primitive type in List.
  4. Compile Properly but Runtime Exception
Question 12 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test { public static void main(String[] args) { Integer i = null; int j = i; System.out.println(j); } }

  1. 0
  2. Compile with error
  3. null
  4. NullPointerException
Question 13 Multiple Choice (Single Answer)

What is the output for the below code ? public class Outer { private int a = 7; class Inner { public void displayValue() { System.out.println("Value of a is " + a); } } } public class Test { public static void main(String... args) throws Exception { Outer mo = new Outer(); Outer.Inner inner = mo.new Inner(); inner.displayValue(); } }

  1. Value of a is 7
  2. Compile Error - not able to access private member.
  3. Runtime Exception
  4. Value of a is 8
Question 14 Multiple Choice (Single Answer)

What is the output for the below code ? public class B { public String getCountryName(){ return "USA"; } public StringBuffer getCountryName(){ StringBuffer sb = new StringBuffer(); sb.append("UK"); return sb; } public static void main(String[] args){ B b = new B(); System.out.println(b.getCountryName().toString()); } }

  1. Compile with error
  2. USA
  3. UK
  4. Runtime Exception
Question 15 Multiple Choice (Single Answer)

What is the output for the below code ? public class C { } public class D extends C{ } public class A { public C getOBJ(){ System.out.println("class A - return C"); return new C(); } } public class B extends A{ public D getOBJ(){ System.out.println("class B - return D"); return new D(); } } public class Test { public static void main(String... args) { A a = new B(); a.getOBJ(); } }

  1. Compile with error - Not allowed to override the return type of a method with a subtype of the original type.
  2. class A - return C
  3. class B - return D
  4. Runtime Exception
Question 16 Multiple Choice (Single Answer)

What is the output for the below code ? import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String... args) { Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("b"); boolean b = m.matches(); System.out.println(b); } }

  1. true
  2. compile error
  3. false
  4. b
Question 17 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test { public static void main(String... args) { String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input).useDelimiter("\sfish\s"); System.out.println(s.nextInt()); System.out.println(s.nextInt()); System.out.println(s.next()); System.out.println(s.next()); s.close(); } }

  1. 1 2 red blue
  2. Compile Error - because Scanner is not defind in java.
  3. 1 fish 2 fish red fish blue fish
  4. 1 fish 2 fish red blue fish
Question 18 Multiple Choice (Single Answer)

Flex is used for the development of Rich Internet Application which can also be deployed in

  1. Desktop
  2. Browsers
  3. Operating systems
  4. All the above
Question 19 Multiple Choice (Multiple Answers)

What are the languages the flex can be developed?

  1. ActionScript
  2. MXML
  3. HTML
  4. None of the above
Question 20 True/False

Is it possible to make httpService Requests synchronous

  1. True
  2. False