0

programming languages Online Quiz - 17

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

The statement that is used to replace multiple if statements is

  1. a. The switch and case statement

  2. b. ternary operator

  3. c. nested if statement

  4. d. The #endif statement


Correct Option: A

AI Explanation

To answer this question, you need to understand the purpose of each statement and how they are used to make decisions in programming.

Option A) The switch and case statement - This option is correct because the switch and case statement is often used to replace multiple if statements when there are multiple possible values for a variable. It allows the program to execute different blocks of code based on the value of a single variable.

Option B) The ternary operator - This option is incorrect because the ternary operator is used as a shorthand way of writing if-else statements. It is used when there are only two possible outcomes based on a condition.

Option C) The nested if statement - This option is incorrect because the nested if statement is used when there is a need to check multiple conditions within one if statement. It is used to create a hierarchy of conditions.

Option D) The #endif statement - This option is incorrect because the #endif statement is used in conditional compilation to end a block of code that is only compiled if a certain condition is met. It is not used to replace multiple if statements.

Therefore, the correct answer is A) The switch and case statement.

The following code will generate a compiler error.

string GetAgePhrase(int age) {
    if (age > 60) return "Senior";
    if (age > 40) return "Middle-aged";
    if (age > 20) return "Adult";
    if (age > 12) return "Teen-aged";
    if (age > 4) return "Toddler";
}

Which of the following statements, inserted as the last line of the function, would solve the problem?

  1. a. continue;

  2. b. break;

  3. c. return "Infant";

  4. d. return 0;


Correct Option: C
static void Main(string[] args) {
    try {
        Console.WriteLine("Level 1");
        try {
            Console.WriteLine("Level 2");
            goto exit;
        } finally {
            Console.WriteLine("Level 2 Finished");
        }
    } finally {
        Console.WriteLine("Level 1 Finished");
    }
    exit: ;
}

What is the output?

  1. a. Leve1,Level 2,Level 1 Finished

  2. b. Level,Leve2,Leve 2 Finished

  3. c. Level 1,Level 2,Leve 2 Finished,Level 1 Finished

  4. d.Error


Correct Option: C
Explanation:

To understand the output of this code, we need to examine the nested try-finally blocks and the use of the goto statement.

The code first prints "Level 1", then enters a try block. Within that try block, it prints "Level 2", then jumps to the "exit" label using a goto statement. This causes the finally block within the inner try block to execute, which prints "Level 2 Finished".

After the inner finally block completes, the outer finally block executes, which prints "Level 1 Finished". Thus, the final output of the code will be:

Level 1 Level 2 Level 2 Finished Level 1 Finished

Therefore, the correct answer is:

The Answer is: C. Level 1, Level 2, Level 2 Finished, Level 1 Finished

what is the default access specifier for a Top-level Class, which are not nested into other Classes?

  1. a. public

  2. b. private

  3. c. protected

  4. d. internal


Correct Option: D

Which statement is true about interface and abstract classes?

  1. a. An abstract class may only contain incomplete methods (abstract methods)

  2. b. An interface may contain complete or incomplete methods

  3. c. A class may inherit several interfaces, A class may inherit only one abstract class

  4. d. A class implementing an abstract class has to implement all the methods of the abstract class, but the same is not required in the case of an interface


Correct Option: C

If a method is marked as protected internal who can access it?

  1. a. Access is limited to the current assembly

  2. b. Access is limited to the containing class or types derived from the containing class.

  3. c. Access is limited to the containing type

  4. d. Access is limited to the current assembly or types derived from the containing class.


Correct Option: D
Explanation:

To understand who can access a method marked as protected internal, the user needs to know about access modifiers in C#.

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

A. Access is limited to the current assembly: This option is partially correct. When a method is marked as internal, it can only be accessed within the same assembly. However, when a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option A is incorrect.

B. Access is limited to the containing class or types derived from the containing class: This option is incorrect. When a method is marked as protected, it can be accessed by the containing class or types derived from the containing class. However, when a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option B is incorrect.

C. Access is limited to the containing type: This option is incorrect. protected internal access modifier provides more accessibility than protected access modifier. When a method is marked as protected, it can be accessed by the containing class or types derived from the containing class. However, when a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option C is incorrect.

D. Access is limited to the current assembly or types derived from the containing class: This option is correct. When a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option D is the correct answer.

The Answer is: D

The default type of enum is integer and has a default value 1.

  1. a. true

  2. b. false

  3. c. sometimes

  4. d. depends


Correct Option: B

Unboxing of a null refrence type will return null.

  1. a. true

  2. b. false

  3. c. an exception is thrown

  4. d. depends


Correct Option: C

can we have protected element as a namespace member? eg namespace mine { protected class test { } }

  1. a. yes,all access specifiers are valid for namespace members

  2. b. No,only private access specifiers are possible

  3. c. No,implicitly all members are protected

  4. d. No,the namespace allows only public and internal elements as its members


Correct Option: D

AI Explanation

To answer this question, you need to understand the concept of access specifiers in C++ and how they apply to namespace members.

In C++, access specifiers (such as public, private, and protected) are used to control the visibility and accessibility of class members. However, access specifiers do not apply to namespace members.

Namespace members are by default accessible to all code within the same namespace. They do not have access specifiers like classes do. Therefore, it is not possible to have a protected element as a namespace member.

Let's go through each option to understand why it is correct or incorrect:

Option A) Yes, all access specifiers are valid for namespace members - This option is incorrect because access specifiers do not apply to namespace members.

Option B) No, only private access specifiers are possible - This option is incorrect because access specifiers do not apply to namespace members. Private access specifier is specific to class members, not namespace members.

Option C) No, implicitly all members are protected - This option is incorrect because access specifiers do not apply to namespace members. The concept of implicit protection is not applicable to namespace members.

Option D) No, the namespace allows only public and internal elements as its members - This option is correct because namespace members are by default accessible to all code within the same namespace. They do not have access specifiers like classes do. The only valid access specifiers for namespace members are public and internal.

The correct answer is D) No, the namespace allows only public and internal elements as its members. This option is correct because access specifiers do not apply to namespace members.

Can multiple catch blocks be executed for single try block?

  1. a. Yes

  2. b. No

  3. c. Under certain conditions

  4. d. May be


Correct Option: B

AI Explanation

To answer this question, you need to understand how try-catch blocks work in programming.

In general, when an exception is thrown within a try block, the program jumps to the corresponding catch block that can handle that exception. Once the catch block is executed, the program continues with the code after the catch block.

In the case of multiple catch blocks, only one catch block will be executed for a single try block. The catch block that is executed will be the first catch block that matches the type of the exception thrown. Once that catch block is executed, the program will not execute any subsequent catch blocks.

Therefore, the correct answer is:

B. No - Multiple catch blocks cannot be executed for a single try block.

Difference between const and readonly

  1. a. readonly value can be changed later

  2. b. const items are dealt at compile time however readonly is dealt at runtime

  3. c. const value can be changed later

  4. d. const items are dealt at runtime however readonly is dealt at compile time


Correct Option: B

A variable declared inside a method is called

  1. a. local variable

  2. b. global variable

  3. c. static variable

  4. d. private variable


Correct Option: A

In programming languages, which language's version name is also known as "Scorpio" ?

  1. JAVA

  2. C

  3. ColdFusion

  4. HTML 5


Correct Option: C
  1. a, b

  2. b, c

  3. a, d

  4. a, e


Correct Option: D
Explanation:

To solve this question, the user needs to know about abstract classes and abstract methods in Java.

An abstract class is a class that cannot be instantiated on its own and is often used as a template for other classes. Abstract methods are methods that are declared but have no implementation in the abstract class.

Option a declares an abstract class Digit with an abstract method print(). This is a valid declaration of an abstract class.

Option b declares a class Digit with an abstract method print(). This is not a valid declaration because a non-abstract class cannot have an abstract method.

Option c declares an abstract class Digit with an abstract method print() that has an empty implementation. This is not valid because an abstract method cannot have an implementation in the abstract class.

Option d declares an abstract class Digit with a non-abstract method print(). This is not valid because an abstract class must have at least one abstract method.

Option e declares a non-abstract class Digit with a non-abstract method print(). This is a valid declaration of a non-abstract class.

Therefore, the correct answer is:

The Answer is: D

The following code contains a compilation error , what can be done to fix this error – independently?

abstract class AirPlane 
{ // line 1  
    abstract void fly(); // line 2  
    void land()
    {
        System.out.print("Landing..");
    }
}
class AirJet extends AirPlane 
{ // line 10  
    AirJet()
    {
        super(); // line 13  
    }
    void fly() 
    {
        System.out.print("Flying..");
    }
    abstract void land(); // line 20 
}

Please choose all the answers that apply:
a) Remove abstract from line 20 and add body to method land()

b) Declare class AirJet as abstract to at line 10

c) Remove super() call at line 13

d) Remove abstract at line 1 and line 2

  1. a, b

  2. a, c

  3. b, c

  4. c, d


Correct Option: A
Explanation:

To fix the compilation error in the given code, we need to understand the concept of abstract classes and methods in Java.

An abstract class is a class that cannot be instantiated, and it can have abstract methods, which are declared without an implementation. Any subclass of an abstract class must either implement all the abstract methods of its parent class or be declared as abstract itself.

In the given code, we have an abstract class AirPlane and its subclass AirJet. AirPlane has one abstract method fly() and one non-abstract method land(), while AirJet overrides the fly() method and declares another abstract method land().

The compilation error in the code is due to the fact that AirJet is not implementing all the abstract methods of its parent class AirPlane. To fix this error, we have two options:

a) Remove abstract from line 20 and add body to method land()

This option is correct. Since AirJet is a concrete class, it must implement all the abstract methods of its parent class AirPlane. Therefore, we can remove the abstract keyword from the declaration of land() method and provide an implementation for it.

b) Declare class AirJet as abstract to at line 10

This option is incorrect. Declaring AirJet as abstract will not solve the compilation error. In fact, it will introduce another error because AirJet will now have two abstract methods, but it is not declaring itself as abstract.

c) Remove super() call at line 13

This option is incorrect. Removing the super() call will not solve the compilation error. In fact, it will introduce another error because AirPlane does not have a default constructor, and super() is trying to call it.

d) Remove abstract at line 1 and line 2

This option is incorrect. Removing the abstract keyword from line 1 and line 2 will not solve the compilation error. In fact, it will introduce another error because now AirPlane will have a non-implemented method.

Therefore, the correct answer is:

The Answer is: A. a, b

What are the rules to implement an interface? Considering the following declaration for interface Convertable, which of the following code segments will compile?

public interface Convertable {
    int convertToInt();
    char convertToChar();
}

a)

class Digit implements Convertable {
    public char convertToChar() {
        return 0;
    }
    public int convertToInt() {
        return 0;
    }
}

b)

 abstract class Digit implements Convertable {
     int convertToInt();
     char convertToChar();
 }

c)

abstract class Digit implements Convertable {
    public int convertToInt() {
        return 0;
    }
}

d)

abstract class Digit implements Convertable {
    public int convertToInt() {
        return 0;
    }
    char convertToChar();
}

e)

  class Digit implements Convertable {
    int convertToInt() {
        return 0;
        return 0;
    }
}

f)

interface Roundable extends Convertable {
    int roundUp();
}

  1. a, b, c

  2. a, d, e

  3. a, c, f

  4. a, d, f


Correct Option: C

Which of the following variables is incorrectly declared?

public abstract interface Bouncable {
    int a = 0;
    public int b = 1;
    public static int c = 2;
    public static transient int d = 3;
    public final int e = 3;
    public static final int f = 3;
}
  1. c

  2. d

  3. e

  4. f


Correct Option: B

Three of the methods are incorrectly declared, which are they?

public abstract class Tester {
    public void test1();
    public final void test2() {};
    public static void test3() {};
    public abstract static void test4();
    public abstract final void test5();
}
  1. test1, test2 and test4

  2. test2, test4 and test5

  3. test1, test4 and test5

  4. test1, test2 and test3


Correct Option: C

Is the following declaration for interface Bendable correct and free of compilation error?

abstract interface Bendable { // line 1   

    final int x = 2009; // line 3    
    
    void method1() {}; // line 5 
}
  1. Yes, this is a correct and free of error declaration

  2. No, compilation error at line 1

  3. No, compilation error at line 3

  4. No, compilation error at line 5


Correct Option: D

What is the result of compiling and running the following code?

public class Tester {
    public static void main(String[] args) {
        System.out.print("1");
        try {
            return;
        } catch (Exception e) {
            System.out.print("2");
        } finally {
            System.out.print("3");
        }
        System.out.print("4");
    }
}
  1. 1234

  2. 13

  3. 1

  4. Compilation Error


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) 1234 - This option is incorrect. The code reaches the try block, encounters a return statement, and immediately exits the main method. Therefore, the code does not reach the print statement with "4", so the output does not include "4".

Option B) 13 - This option is correct. The code first prints "1". Then, it encounters a return statement in the try block, which causes the program to exit the main method. However, before exiting, the finally block is executed, resulting in the print statement with "3". Therefore, the output is "13".

Option C) 1 - This option is incorrect. The code does not reach the print statement with "4", so the output does not include "4".

Option D) Compilation Error - This option is incorrect. The code does not have any compilation errors.

The correct answer is B. This option is correct because the code prints "1" and "3", resulting in the output "13".

- Hide questions