0

programming languages Online Quiz - 43

Description: programming languages Online Quiz - 43
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

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


Correct Option: C

: 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


Correct Option: D

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


Correct Option: D

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


Correct Option: C

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


Correct Option: C

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


Correct Option: C

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


Correct Option: A

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


Correct Option: D

AI Explanation

To answer this question, we need to understand the concept of multithreading and the behavior of threads in Java.

In the given code, we have a class A that implements the Runnable interface. The Runnable interface provides a common protocol for objects that wish to execute code concurrently. The run() method of class A prints the name of the current thread using the Thread.currentThread().getName() method.

In the Test class, we create two instances of class A, a and t1. We then create two Thread objects, t and t1, and pass the instance a as the target for both threads. We set the names of t and t1 to "t" and "t1" respectively, using the setName() method. We also set the priority of t to 10 and the priority of t1 to -3 using the setPriority() method.

Finally, we start both threads using the start() method.

Now, let's go through each option to understand why it is correct or incorrect:

Option A) t t1 - This option implies that the output will be "t t1". However, the actual output may vary because the execution of threads is non-deterministic. The order in which the threads are executed is not guaranteed.

Option B) t1 t - This option implies that the output will be "t1 t". However, as mentioned earlier, the actual output may vary.

Option C) t t - This option implies that the output will be "t t". However, as mentioned earlier, the actual output may vary.

Option D) Compilation succeeds but Runtime Exception - This option is the correct answer. The code will compile successfully, but at runtime, it may throw a NullPointerException. This is because we are passing the same instance a to both t and t1. When multiple threads are accessing the same object concurrently, there can be synchronization issues. In this case, since the run() method of class A is not synchronized, both threads can access it concurrently, leading to a potential NullPointerException when accessing Thread.currentThread().

Therefore, the correct answer is D) 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. }

  1. good

  2. null

  3. Compilation fails with an error at line 5

  4. Compilation succeed but Runtime Exception


Correct Option: A

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


Correct Option: A

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


Correct Option: A

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


Correct Option: A

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


Correct Option: A

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


Correct Option: C

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


Correct Option: A

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("\s*fish\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


Correct Option: A
Explanation:

To solve this question, the user needs to understand how the Scanner class works in Java and how the useDelimiter() method works.

The useDelimiter() method sets the delimiter pattern for the Scanner object to use when breaking down input into tokens. In this case, the delimiter pattern is set to "\s*fish\s*", which means that the Scanner object will split the input string whenever it encounters the word "fish" surrounded by any number of whitespace characters.

The code then uses the nextInt() and next() methods of the Scanner object to retrieve the tokens that were split from the input string.

Let's go through each option to determine the correct answer:

A. 1 2 red blue: This option is correct. The first call to nextInt() retrieves the integer "1" that comes before the first occurrence of "fish". The second call to nextInt() retrieves the integer "2" that comes after the first occurrence of "fish". The first call to next() retrieves the word "red", and the second call to next() retrieves the word "blue".

B. Compile Error - because Scanner is not defined in java.: This option is incorrect. The Scanner class is defined in Java, so this code will not produce a compile error due to the use of Scanner.

C. 1 fish 2 fish red fish blue fish: This option is incorrect. The code does not print out the entire input string, but rather it retrieves specific tokens from the input string using the nextInt() and next() methods.

D. 1 fish 2 fish red blue fish: This option is incorrect. The second call to nextInt() retrieves the integer "2", which comes after the first occurrence of "fish". The first call to next() retrieves the word "red", and the second call to next() retrieves the word "blue".

Therefore, the correct answer is:

The Answer is: A. 1 2 red blue

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


Correct Option: D

What are the languages the flex can be developed?

  1. ActionScript

  2. MXML

  3. HTML

  4. None of the above


Correct Option: A,B

Is it possible to make httpService Requests synchronous

  1. True

  2. False


Correct Option: B
- Hide questions