0

programming languages Online Quiz - 322

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

12 You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java //Base.java package Base; class Base{ protected void amethod(){ System.out.println("amethod"); }//End of amethod }//End of class base package Class1; //Class1.java public class Class1 extends Base{ public static void main(String argv[]){ Base b = new Base(); b.amethod(); }//End of main }//End of Class1

  1. a. Compile Error: Methods in Base not found

  2. b. Compile Error: Unable to access protected method in base class

  3. c. Compilation followed by the output "amethod"

  4. d. Compile error: Superclass Class1.Base of class Class1.Class1 not found


Correct Option: D

13 Which of the following statements are true?

  1. a. The elements in a Java array can only be of primitive types, not objects

  2. b. Arrays elements are initialized to default values wherever they are created using the keword new

  3. c. An array may be dynamically resized using the setSize method

  4. d. You can find out the size of an array using the size method


Correct Option: B

14 Which of the following are legal statements?

  1. a float f=1/3;

  2. b int i=1/3;

  3. c float f=1.01;

  4. d double d=999d;


Correct Option: A,B,D

AI Explanation

To determine which of the statements are legal, we need to consider the syntax rules for variable declaration and assignment in programming languages.

Let's go through each option to understand whether it is a legal statement or not:

Option A) a float f=1/3; - This statement is legal. It declares a variable "f" of type float and assigns it the value 1/3. In many programming languages, the division operation between two integers would result in an integer value, but since "f" is of type float, the result will be a floating-point number.

Option B) b int i=1/3; - This statement is not legal. It attempts to declare a variable "i" of type int and assigns it the value 1/3. Similar to option A, the division operation would result in an integer value, but the variable type is int, which cannot store decimal values.

Option C) c float f=1.01; - This statement is legal. It declares a variable "f" of type float and assigns it the value 1.01. The value assigned is a valid floating-point number.

Option D) d double d=999d; - This statement is legal. It declares a variable "d" of type double and assigns it the value 999d. The "d" suffix indicates that the value is of type double.

Therefore, the correct options are A, B, and D.

15 Which of the following are valid statements?

  1. A System.out.println(1+1);

  2. B int i=2+'2';

  3. C String s="on"+'one';

  4. D byte b=255;


Correct Option: A,B

AI Explanation

To determine which of the statements are valid, let's go through each option:

A. A System.out.println(1+1); This statement is valid because it uses the System.out.println() method to print the result of the expression 1+1, which is 2.

B. B int i=2+'2'; This statement is not valid. In Java, when you use single quotes ('') around a character, it represents a single character. In this case, '2' is not a valid character. If you want to concatenate the integer value 2 with the string "2", you should use double quotes ("") instead of single quotes.

C. C String s="on"+'one'; This statement is not valid. Similar to the previous option, the use of single quotes around 'one' is not valid. If you want to concatenate the string "on" with the string "one", you should use double quotes for both strings.

D. D byte b=255; This statement is not valid. In Java, the range of values that can be assigned to a byte is from -128 to 127. The value 255 is outside this range.

Based on the explanations above, the valid statements are A and B. Therefore, the correct answer is A, B.

16 Which of the following statements are true?

  1. a. All of the variables in an interface are implicitly static

  2. b. All of the variables in an interface are implicitly final

  3. c. All of the methods in an interface are implicitly abstract

  4. d. A method in an interface can access class level variables


Correct Option: A,B,C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) All of the variables in an interface are implicitly static - This option is true. In an interface, all variables are implicitly static, meaning they belong to the interface itself rather than any specific instance.

Option B) All of the variables in an interface are implicitly final - This option is true. In an interface, all variables are implicitly final, meaning their values cannot be changed once they are assigned.

Option C) All of the methods in an interface are implicitly abstract - This option is true. In an interface, all methods are implicitly abstract, meaning they do not have a body and must be implemented by any class that implements the interface.

Option D) A method in an interface can access class-level variables - This option is false. Methods in an interface can only access variables declared within the interface itself. They cannot access variables from implementing classes or any other classes.

The correct answer is A, B, C. These options are correct because variables in an interface are implicitly static and final, and methods in an interface are implicitly abstract.

17 Which of the following statements are true?

  1. a. An interface can only contain method and not variables

  2. b. Interfaces cannot have constructors

  3. c. A class may extend only one other class and implement only one interface

  4. d. Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.


Correct Option: B

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) An interface can only contain methods and not variables - This option is incorrect. Interfaces can contain both methods and variables. In Java, interface variables are by default public, static, and final.

Option B) Interfaces cannot have constructors - This option is correct. Interfaces cannot have constructors because they are not meant to be instantiated directly. They are used as a blueprint for classes that implement them.

Option C) A class may extend only one other class and implement only one interface - This option is incorrect. In Java, a class can extend only one other class, but it can implement multiple interfaces. This is known as multiple interface inheritance.

Option D) Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the interfaces - This option is incorrect. Interfaces in Java are used for abstraction and defining contracts. They do not provide functionality themselves. Implementing classes are responsible for providing the implementation of the methods defined in the interface.

The correct answer is B) Interfaces cannot have constructors.

18 You have a public class called myclass with the main method defined as follows public static void main(String parm[]){ System.out.println(parm[0]); } If you attempt to compile the class and run the program as follows java myclass hello What will happen?

  1. a. Compile time error, main is not correctly defined

  2. b. Run time error, main is not correctly defined

  3. c. Compilation and output of java

  4. d. Compilation and output of hello


Correct Option: D

19 Read this piece of code carefully if("String".toString() == "String") System.out.println("Equal"); else System.out.println("Not Equal");

  1. a. the code will compile an print "Equal".

  2. b. the code will compile an print "Not Equal".

  3. c. the code will cause a compiler error.

  4. d.


Correct Option: B

20 Read this piece of code carefully if(" String ".trim() == "String") System.out.println("Equal"); else System.out.println("Not Equal");

  1. a. the code will compile an print "Equal".

  2. b. the code will compile an print "Not Equal".

  3. c. the code will cause a compiler error

  4. d.


Correct Option: A

AI Explanation

To answer this question, let's go through each option and analyze the code snippet provided:

Option A) The code will compile and print "Equal". In the given code snippet, the trim() method is used to remove leading and trailing whitespace from the string " String ". The trim() method returns a new string with the whitespace removed.

However, when comparing strings, the == operator checks for reference equality, not value equality. In Java, the == operator compares the memory addresses of the objects being compared.

In this case, the string " String " is not the same object as the string "String", even though they have the same value after trimming. Therefore, the == comparison will evaluate to false.

So, the code will not print "Equal".

Option B) The code will compile and print "Not Equal". As explained in Option A, the == comparison will evaluate to false because the strings being compared are different objects. Therefore, the code will print "Not Equal".

Option C) The code will cause a compiler error. There is no compilation error in the given code. The code is syntactically correct, and there are no issues that would cause a compiler error.

Option D) (No option provided) This is not a valid option.

The correct answer is A) The code will compile and print "Equal".

Given: 11. public static void main(String[] args) { 12. try { 13. args=null; 14. args[0] = “test”; 15. System.out.println(args[0]); 16. } catch (Exception ex) { 17. System.out.println(”Exception”); 18. } catch (NullPointerException npe) { 19. System.out.println(”NullPointerException”); 20. } 21. } What is the result?

  1. test

  2. Exception

  3. Compilation fails.

  4. NullPointerException


Correct Option: C

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) test - This option is incorrect because the code on line 13 assigns a null value to the args array, and then on line 14, it tries to access args[0] and assign it the value "test". Since args is null, a NullPointerException is thrown before reaching the System.out.println statement.

Option B) Exception - This option is incorrect because the catch block on line 16 catches any exception of type Exception. However, the exception that is thrown in this case is a NullPointerException, so the catch block on line 16 is not executed.

Option C) Compilation fails - This option is correct because on line 14, the code tries to assign a value to args[0] without first initializing the args array. This will result in a compilation error because the args array is null and cannot be accessed.

Option D) NullPointerException - This option is incorrect because the catch block on line 19 catches a NullPointerException, but since the code fails to compile, this block is not executed.

The correct answer is C) Compilation fails. This option is correct because attempting to assign a value to an uninitialized array (args[0]) will result in a compilation error.

Given: 11. static classA { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B “); } 16. } 17. public static void main(String[] args) { 18.A a=new B(); 19. a.process(); 20.} What is the result?

  1. B

  2. The code runs with no output.

  3. An exception is thrown at runtime.

  4. Compilation fails because of an error in line 15.

  5. Compilation fails because of an error in line 18.

  6. Compilation fails because of an error in line 19.


Correct Option: F

Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12. } Which two will produce an AssertionError? (Choose two.)

  1. java test

  2. java -ea test

  3. java test file1

  4. java -ea test file1

  5. java -ea test file1 file2

  6. java -ea:test test file1


Correct Option: B,E

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) java test - This option will not produce an AssertionError because the -ea flag is not used to enable assertions.

Option B) java -ea test - This option will produce an AssertionError because the -ea flag is used to enable assertions. The assertion on line 10 (assert a.length == 1;) will fail if the length of a is not equal to 1.

Option C) java test file1 - This option will not produce an AssertionError because the -ea flag is not used to enable assertions.

Option D) java -ea test file1 - This option will not produce an AssertionError because the -ea flag is used to enable assertions, but the program does not have any assertions to check.

Option E) java -ea test file1 file2 - This option will produce an AssertionError because the -ea flag is used to enable assertions. The assertion on line 10 (assert a.length == 1;) will fail if the length of a is not equal to 1.

Option F) java -ea:test test file1 - This option will not produce an AssertionError because the -ea flag is used to enable assertions, but the program does not have any assertions to check.

The correct answers are B and E. These options will produce an AssertionError because they include the -ea flag to enable assertions, and the assertion on line 10 will fail if the length of a is not equal to 1.

Given: 11. public static Iterator reverse(List list) { 12. Collections.reverse(list); 13. return list.iterator(); 14. } 15. public static void main(String[] args) { 16. List list = new ArrayList(); 17. list.add(” 1”); list.add(”2”); list.add(”3”); 18. for (Object obj: reverse(list)) 19. System.out.print(obj + “,”); 20. } ‘What is the result?

  1. 3,2, 1,

  2. 1, 2, 3,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.


Correct Option: C

Given: 11. public static void main(String[] args) { 12. String str = “null’; 13. if (str == null) { 14. System.out.println(”null”); 15. } else (str.length() == 0) { 16. System.out.println(”zero”); 17. } else { 18. System.out.println(”some”); 19. } 20. } ‘What is the result?

  1. null

  2. zero

  3. some

  4. Compilation fails.

  5. An exception is thrown at runtime.


Correct Option: D

AI Explanation

To answer this question, let's go through each option and analyze the code:

Option A) null - This option is incorrect because the code does not print "null" in this scenario.

Option B) zero - This option is incorrect because the code does not print "zero" in this scenario.

Option C) some - This option is incorrect because the code does not print "some" in this scenario.

Option D) Compilation fails - This option is correct. The code will fail to compile due to a syntax error on line 13. The line String str = "null'; has a syntax error because the closing double quote is missing. It should be String str = "null";.

Option E) An exception is thrown at runtime - This option is incorrect because the code does not throw an exception at runtime. The code fails to compile before it can be executed.

Therefore, the correct answer is D) Compilation fails.

Given: 11. public class Bootchy { 12. int bootch; 13. String snootch; 14. 15. public Bootchy() { 16. this(”snootchy”); 17. System.out.print(”first “); 18. } 19. 20. public Bootchy(String snootch) { 21. this(420, “snootchy”); 22. System.out.print(”second “); 23. } 24. 25. public Bootchy(int bootch, String snootch) { 26. this.bootch = bootch; 27. this.snootch = snootch; 28. System.out.print(”third “); 29. } 30. 31. public static void main(String[] args) { 32. Bootchy b = new Bootchy(); 33. System.out.print(b.snootch +“ “ + b.bootch); 34. } 35. } What is the result?

  1. snootchy 420 third second first

  2. snootchy 420 first second third

  3. first second third snootchy 420

  4. third second first siiootchy 420

  5. third first second snootchy 420

  6. first second first third snootchy 420


Correct Option: D

AI Explanation

To determine the result of the given code, let's analyze the code step by step:

  1. Line 32: Bootchy b = new Bootchy();
  • This line creates a new instance of the Bootchy class and assigns it to the variable b.
  • Since no arguments are passed, it invokes the default constructor public Bootchy().
  1. Line 15: public Bootchy()

    • This is the default constructor.
    • It calls the constructor this("snootchy");.
    • It prints "first ".
  2. Line 20: public Bootchy(String snootch)

    • This constructor takes a String parameter snootch.
    • It calls the constructor this(420, "snootchy");.
    • It prints "second ".
  3. Line 25: public Bootchy(int bootch, String snootch)

    • This constructor takes an int parameter bootch and a String parameter snootch.
    • It assigns the values of bootch and snootch to the respective instance variables.
    • It prints "third ".
  4. Line 33: System.out.print(b.snootch + " " + b.bootch);

    • This prints the value of b.snootch (which is "snootchy") followed by a space, and then the value of b.bootch (which is 0, as it was not explicitly assigned).

Based on the above analysis, the result of the given code is:

D. third second first siiootchy 420

Given: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;

  2. Foo f= (Delta)x;

  3. Foo f= (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;


Correct Option: B

Given That: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;

  2. Foo f= (Delta)x;

  3. Foo f= (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;


Correct Option: B
Explanation:

To solve this question, the user needs to know about inheritance in Java and how casting works. The key to answering this question correctly is understanding how casting works between different classes in an inheritance hierarchy.

Now, let's go through each option and explain why it is right or wrong:

A. Alpha a = x; - This code is valid because Beta is a subclass of Alpha, so it can be upcast to an Alpha without issue. This will not cause a ClassCastException.

B. Foo f= (Delta)x; - This code is invalid because Delta is a subclass of Beta, so it cannot be downcast to Delta from Beta. This will throw a ClassCastException.

C. Foo f= (Alpha)x; - This code is valid because Beta is a subclass of Alpha, so it can be upcast to an Alpha without issue. This will not cause a ClassCastException.

D. Beta b = (Beta)(Alpha)x; - This code is invalid because Alpha is a superclass of Beta, so it cannot be downcast to Beta from Alpha. This will throw a ClassCastException.

Therefore, the correct answer is:

The Answer is: B

Perl was develped by

  1. Larry floor

  2. Larry wall

  3. Denise Ritche

  4. Micheal Jackson


Correct Option: B

Perl is

  1. An interpreter language

  2. A compiler language

  3. Partial compiler and interpreter language

  4. An Indian language


Correct Option: C

File extension for a perl program is

  1. .pl

  2. .per

  3. .perl

  4. .prl


Correct Option: A
- Hide questions