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.
Questions
How many key-value pairs will the hash contain? my %hash = ( [1, 2] => 1, [1, 2] => 2 );
- 1
- 2
- 3
- 4
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";
- empty string
- value
- the code will fail
- None of the above
: What will be the size of the @fields array? my $record = ':a:b:c:'; my @fields = split(':', $record, -1);
- 0
- 1
- 3
- 5
What gets printed? my @a = ([1, 2, 3, 4], [5, 6, 7, 8]); print join(' ', @{$a[1]}[1..3]);
- 1 2 3
- 2 3 4
- 5 6 7
- 6 7 8
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;}
- 2
- 6
- 106
- I don't like undefs
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});
- undef
- 0
- 1
- 2
- code will fail
What will be printed? BEGIN { print 'a'; } END { print 'b'; } BEGIN { print 'c'; } END { print 'd'; }
- abcd
- acbd
- cadb
- code will fail
What will be printed by the code below? my @a = (0, 1, 2); my ($b) = @a; print $b;
- 0
- 1
- 0 1 2
- 2
- 3
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(); } }
- t t1
- t1 t
- t t
- Compilation succeed but Runtime Exception
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. }
- good
- null
- Compilation fails with an error at line 5
- Compilation succeed but Runtime Exception
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); } }
- 59
- Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();
- Compile time error, because can't add primitive type in List.
- Compile Properly but Runtime Exception
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); } }
- 0
- Compile with error
- null
- NullPointerException
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(); } }
- Value of a is 7
- Compile Error - not able to access private member.
- Runtime Exception
- Value of a is 8
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()); } }
- Compile with error
- USA
- UK
- Runtime Exception
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(); } }
- Compile with error - Not allowed to override the return type of a method with a subtype of the original type.
- class A - return C
- class B - return D
- Runtime Exception
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); } }
- true
- compile error
- false
- b
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 2 red blue
- Compile Error - because Scanner is not defind in java.
- 1 fish 2 fish red fish blue fish
- 1 fish 2 fish red blue fish
Flex is used for the development of Rich Internet Application which can also be deployed in
- Desktop
- Browsers
- Operating systems
- All the above
What are the languages the flex can be developed?
- ActionScript
- MXML
- HTML
- None of the above
Is it possible to make httpService Requests synchronous
- True
- False