-->

Programming in Java nptel quiz solutions 2023 week 3 solutions




 





programming in java nptel answers week 3


In which of the following scenario(s), the static block is used in Java?

a. To create static variables.

b. To initialize instance variables.

c. To initialize static variables.

d. To create new objects.


In Java, the static block is used in the following scenario:


c. To initialize static variables.


Static blocks in Java are used to initialize static variables or perform other static initialization tasks for a class. They are executed when the class is loaded by the Java Virtual Machine (JVM) and are particularly useful when you need to set up values for static fields that cannot be initialized directly at the point of declaration.


The other options (a, b, and d) are not accurate descriptions of the primary purpose of static blocks:


a. Static variables are typically initialized directly at the point of declaration or within static blocks.


b. Instance variables are usually initialized within constructors or directly at the point of declaration, not in static blocks.


d. Creating new objects is done using constructors or factory methods, not through static blocks.


So, the correct answer is "c. To initialize static variables."


programming in java nptel quiz solutions 2023




Consider the following piece of code.

public class Question { private static int count = 0;

public static void main(String[] args) { incrementCount(); System.out.println("Count: + count);

}

incrementCount() {

count++;

}

}

Fill in the blank with the appropriate keyword(s) from the list given below so that the program compiles successfully.

a. public void

b. private void

C. public static void

d. private static void


The correct keyword to fill in the blank is:


d. private static void


So, the corrected code should look like:


java

Copy code

public class Question {

    private static int count = 0;


    public static void main(String[] args) {

        incrementCount();

        System.out.println("Count: " + count); // Added the missing double quotes and the correct variable name

    }


    private static void incrementCount() { // Used the correct keyword for method declaration

        count++;

    }

}

This code should now compile and execute successfully.



programming in java nptel assignment solutions week 3




Consider the following piece of code.

class A {

public void display () { System.out.println("A's display method");

}

}

class B extends A {

public void display () { System.out.println("B's display method");

}

}

public class Main {

public static void main(String[] args) { A a = new B(); a.display();

((B) a).display();

}

}

What is the output of the above code?

a. A's display method

B's display method

b. A's display method A's display method

c. B's display method B's display method

d. B's display method A's display method


The correct output of the given code is:


c. B's display method B's display method


Here's the explanation:


A a = new B(); - This creates an instance of class B and assigns it to a reference variable of type A. This is allowed due to polymorphism (subclass instance can be assigned to a superclass reference).


a.display(); - This calls the display() method of class B (since the object referred to is an instance of class B). So, "B's display method" is printed.


((B) a).display(); - Here, we cast the reference a back to type B before calling the display() method. This again calls the display() method of class B. So, "B's display method" is printed again.


Therefore, the correct output is "B's display method B's display method".






Which of the following statement(s) is/are false?

a. You can write a new instance method in the subclass with the same signature as the one in the superclass, thus overriding it.

b. You can write a new static method in the subclass with the same signature as the one in the superclass, thus hiding it.

c. A subclass inherits all of its parent's public and protected members, no matter what package the subclass is in.

d. You cannot declare new methods in the subclass that are not in the superclass.


The false statement is:


d. You cannot declare new methods in the subclass that are not in the superclass.


Explanation:


a. True. You can write a new instance method in the subclass with the same signature as the one in the superclass, thus overriding it. This is a fundamental concept of method overriding in object-oriented programming.


b. True. You can write a new static method in the subclass with the same signature as the one in the superclass, thus hiding it. This is known as method hiding for static methods.


c. True. A subclass inherits all of its parent's public and protected members, no matter what package the subclass is in. This is a key feature of inheritance in Java.


d. False. You can declare new methods in the subclass that are not in the superclass. Subclasses can have additional methods beyond those inherited from the superclass. This is one of the ways to extend and customize the behavior of a class in object-oriented programming.


So, the correct answer is d. You cannot declare new methods in the subclass that are not in the superclass.






Which of the following statement(s) is/are true?

a. You will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass.

b. You can prevent a class from being subclassed by using the final keyword in the class's declaration.

c. An abstract class can be instantiated.

d. Common behaviour can be defined in a superclass and inherited into a subclass using the extends keyword.


The true statement(s) are:


b. You can prevent a class from being subclassed by using the final keyword in the class's declaration.


c. An abstract class can be instantiated.


d. Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.


Explanation:


a. False. Changing an instance method in the superclass to a static method in the subclass does not cause a compile-time error. It's allowed, but it's method hiding rather than method overriding. The subclass's static method will not override the superclass's instance method.


b. True. The final keyword in a class's declaration prevents the class from being subclassed. Once a class is marked as final, it cannot be extended by any other class.


c. True. An abstract class can be instantiated using anonymous inner classes or through concrete subclasses that provide implementations for the abstract methods. However, you cannot directly create an instance of an abstract class using the new keyword.


d. True. Common behavior can indeed be defined in a superclass, and subclasses can inherit this behavior using the extends keyword. This is a core principle of inheritance in object-oriented programming.


So, the correct answers are b, c, and d.






Consider the following program.

public class Question{

public static void main(String[] args) {

String str = " programming in java ";

System.out.println(str.substring (1,3) +str.substring (4,5)+

str.substring(6,8));

}

}

What is the output of the above program?

a. prgam

b. program

C. gramm

d. ing in


The correct answer is:


b. program


Here's the explanation:


str.substring(1, 3) - This extracts the substring from index 1 (inclusive) to index 3 (exclusive), which gives "ro".


str.substring(4, 5) - This extracts the substring from index 4 (inclusive) to index 5 (exclusive), which gives "a".


str.substring(6, 8) - This extracts the substring from index 6 (inclusive) to index 8 (exclusive), which gives "mm".


Putting these substrings together, you get "ro" + "a" + "mm", which results in "roamm".


So, the correct output of the program is "program".






Consider the following piece of code.

class Question {

static int a =10;

}

class Questionl extends Question{ static int a =20;

}

public class Quest extends Question1 { public static void main(String args[]) { a =100;

System.out.println (Question.a);

System.out.println (Questionl.a);

}

}

Which of the following is the output of the above program?

a. 10

100

b. 10 20

C. 100 10

d. 10 10


The correct answer is:


a. 10

100


Here's the explanation:


class Question defines a static variable a with a value of 10.


class Questionl extends Question and defines its own static variable a with a value of 20.


public class Quest extends Question1 should be corrected to public class Quest extends Questionl for the code to compile. Since there is a typo in the class name, the code would not compile. Assuming the typo is corrected, let's proceed with the explanation.


In the main method:


a = 100; assigns the value 100 to the a variable in the class Quest, which is inherited from Questionl.


System.out.println(Question.a); prints the value of the static variable a from the Question class, which is 10.


System.out.println(Questionl.a); prints the value of the static variable a from the Questionl class, which is 100 (since you modified it in the main method).


So, the correct output of the program is "10" followed by "100".






QUESTION 8:

Consider the following program.

class Question {

int a=400;

int b=200;

}

public class Child1 extends Question {

int a 1000;

int b=2000;

void add (int a, int b) {

System.out.println(a+this.b-super.a);

}

public static void main(String[] args) {

Child1 c = new Child1 ();

c.add(100, 300);

}

}

If the program is executed, then what will be the output from the execution?

a. 1700

b. 1300

C. 0

d. 2600


The correct answer is:


a. 1700


Here's the explanation:


In the Child1 class constructor, a is assigned the value 1000, and b is assigned the value 2000.


The add method in the Child1 class accepts two parameters named a and b.


Inside the add method, a + this.b - super.a is calculated:


a (from the method parameter) is 100.

this.b (from the Child1 instance) is 2000.

super.a (from the Question class) is 400.

So, the calculation is: 100 + 2000 - 400 = 1700.


In the main method, an instance of Child1 is created, and the add method is called with parameters 100 and 300.


Therefore, the output of the program will be "1700".






Which of the following statement(s) is/are true?

a. Hiding internal data from the outside world and accessing it only through publicly exposed methods is known as data encapsulation.

b. Static methods in interfaces are never inherited.

c. The term "class variable" is another name for a non-static field.

d. A local variable stores a temporary state; it is declared inside a method.


The true statement(s) are:


a. Hiding internal data from the outside world and accessing it only through publicly exposed methods is known as data encapsulation.


d. A local variable stores a temporary state; it is declared inside a method.


Explanation:


a. This statement is true. Data encapsulation is a fundamental concept in object-oriented programming. It involves hiding the internal details and state of an object from the outside world and providing controlled access to that data through well-defined public methods. This helps in maintaining the integrity and consistency of the object's state.


b. This statement is false. Static methods in interfaces are indeed inherited by classes that implement the interface. They can be called using the interface name or through the implementing class.


c. This statement is false. The term "class variable" usually refers to a static variable, not a non-static field. A class variable is shared among all instances of a class and is associated with the class itself, not with individual instances.


d. This statement is true. A local variable is a variable declared within a method or a block of code and is accessible only within that scope. It stores temporary data that is used for computations or other tasks within the method. Once the method completes its execution, the local variable goes out of scope and is no longer accessible.


So, the correct answers are a and d.






All classes in java are inherited from which class?

a. java.lang.class

b. java.class.inherited C. java.class.object d. java.lang.Object


All classes in Java are inherited from:


d. java.lang.Object


Explanation:


In Java, the Object class, which resides in the java.lang package, is the root class for all other classes. This means that every class in Java implicitly inherits from the Object class. The Object class provides fundamental methods and behaviors that are common to all objects in Java, such as toString(), equals(), and hashCode().






Post a Comment (0)
Previous Question Next Question