0% found this document useful (0 votes)
31 views6 pages

Week 3 Java Assignment Overview

The document outlines the details of an assignment for a Programming in Java course, specifically for Week 3. It includes multiple-choice questions regarding Java programming concepts, such as static methods, final variables, and inheritance. The document also notes that the due date for submitting the assignment has passed and records that the user has not submitted it.

Uploaded by

kalidasji210
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Week 3 Java Assignment Overview

The document outlines the details of an assignment for a Programming in Java course, specifically for Week 3. It includes multiple-choice questions regarding Java programming concepts, such as static methods, final variables, and inheritance. The document also notes that the due date for submitting the assignment has passed and records that the user has not submitted it.

Uploaded by

kalidasji210
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

13/11/2024, 15:25 Programming in Java - - Unit 6 - Week 3 :

([Link] ([Link]

vinamrasrivastava112@[Link] 

NPTEL ([Link] » Programming in Java (course)

Course Week 03: Assignment 03


outline The due date for submitting this assignment has passed.
Due on 2024-08-14, 23:59 IST.
About As per our records you have not submitted this assignment.
NPTEL ()
1) What will be the output of the following program? 1 point

How does an
NPTEL
online class First {
course static void staticMethod() {
work? ()
[Link]("Static Method");
Week 0 : () }
}
Week 1 : ()

Week 2 : () class MainClass {


public static void main(String[] args) {
Week 3 : ()
First first = null;
Lecture 11 : [Link]();
Java Static
Scope Rule
}
(unit? }
unit=34&lesso
n=35)
a. Static Method
Lecture 12 : b. Throws a NullPointerException
Demonstration
-V (unit?
c. Compile-time error
unit=34&lesso d. Run time error
n=36)
No, the answer is incorrect.
Lecture 13 :
Inheritance

[Link] 1/6
13/11/2024, 15:25 Programming in Java - - Unit 6 - Week 3 :

(unit? Score: 0
unit=34&lesso Accepted Answers:
n=37) a. Static Method

Lecture 14 : 2) What will be the output of the below program. 1 point


Demonstration
-VI (unit?
unit=34&lesso
class superDemoClass {
n=38)
final int a = 20;
Lecture 15 :
Information }
Hiding (unit?
unit=34&lesso
n=39) class subDemoClass extends superDemoClass {

Week 03 :
void subFunc() {
Programming a = 40;
Assignment 1
[Link]("value of a = " + a);
(/noc24_cs105
/progassignme }
nt?name=376)
}
Week 03 :
Programming
Assignment 2 class demo {
(/noc24_cs105
public static void main(String[] args) {
/progassignme
nt?name=377) subDemoClass subc = new subDemoClass();

Week 03 :
[Link]();
Programming }
Assignment 3
(/noc24_cs105
}
/progassignme
nt?name=378) a. value of a = 20

Week 03 : b. error: cannot assign a value to final variable ‘a’


Programming c. error: unknown variable 'a' in class subDemoClass
Assignment 4
d. value of a = 40
(/noc24_cs105
/progassignme No, the answer is incorrect.
nt?name=379) Score: 0
Accepted Answers:
Week 03 : b. error: cannot assign a value to final variable ‘a’
Programming
Assignment 5
3) All the variables of interface should be? 1 point
(/noc24_cs105
/progassignme
a. default and final
nt?name=380)
b. default and static
Quiz: Week
c. public, static and final
03:
Assignment d. protect, static and final
03
No, the answer is incorrect.
(assessment? Score: 0
name=391)
Accepted Answers:
Week 03 : c. public, static and final
Assignment

[Link] 2/6
13/11/2024, 15:25 Programming in Java - - Unit 6 - Week 3 :

Solution (unit? 4) What will be the output of the below program. 1 point
unit=34&lesso
n=433)

class static_out {
Week 4 : ()
static int x;
Week 5 : () static int y;

Week 6 : ()
void add(int a, int b) {
Week 7 : () x = a + b;
y = x + b;
Week 8 : ()
}
Week 9 : () }

Week 10 : ()
public class static_use {
Week 11 : () public static void main(String args[]) {
static_out obj1 = new static_out();
Week 12 : ()
static_out obj2 = new static_out();
Books () int a = 2;
[Link](a, a + 1);
Text
Transcripts [Link](5, a);
() [Link](obj1.x + " " + obj2.y);
}
}

a. 7 7.4
b. 6 6.4
c. 7 9
d. 9 7

No, the answer is incorrect.


Score: 0
Accepted Answers:
c. 7 9

5) What will be the output of the following Java code? 1 point

class access {
public int x;
private int y;

void cal(int a, int b) {


x = a + 1;

[Link] 3/6
13/11/2024, 15:25 Programming in Java - - Unit 6 - Week 3 :

y = b;
}

void print() {
[Link](" " + y);
}
}

public class access_specifier {


public static void main(String args[]) {
access obj = new access();
[Link](2, 3);
[Link](obj.x);
[Link]();
}
}

a. 2 3
b. 3 3
c. Runtime Error
d. Compilation Error

No, the answer is incorrect.


Score: 0
Accepted Answers:
b. 3 3

6) If a variable of primitive datatype in Java is declared as final, then 1 point

a. It cannot get inherited


b. Its value cannot be changed
c. It cannot be accessed in the subclass
d. All of the above

No, the answer is incorrect.


Score: 0
Accepted Answers:
b. Its value cannot be changed

7) Members which are not intended to be inherited are declared as 1 point

a. Public members
b. Protected members
c. Private members
d. Private or Protected members

No, the answer is incorrect.

[Link] 4/6
13/11/2024, 15:25 Programming in Java - - Unit 6 - Week 3 :

Score: 0
Accepted Answers:
c. Private members

8) If a base class is inherited in protected access mode then which among the 1 point
following is true?

a. Public and Protected members of base class becomes protected members of derived
class
b. Only protected members become protected members of derived class
c. Private, Protected and Public all members of base, become private of derived class
d. Only private members of base, become private of derived class

No, the answer is incorrect.


Score: 0
Accepted Answers:
a. Public and Protected members of base class becomes protected members of derived class

9) Which type of inheritance leads to diamond problem? 1 point

a. Single level
b. Multi-level
c. Multiple
d. Hierarchical

No, the answer is incorrect.


Score: 0
Accepted Answers:
c. Multiple

10) What will be the output of the below program: 1 point

class superDemoClass {

final void func() {


int a = 20;
[Link]("value of a = " + a);
}
}

class subDemoClass extends superDemoClass {


void func() {
int b = 60;
[Link]("value of b = " + b);

}
}

class demo {

[Link] 5/6
13/11/2024, 15:25 Programming in Java - - Unit 6 - Week 3 :

public static void main(String[] args) {


subDemoClass subc = new subDemoClass();
[Link]();

}
}

a. error: func() in subDemoClass cannot override func() in superDemoClass


b. value of b = 60
c. value of a = 20
d. None of the above

No, the answer is incorrect.


Score: 0
Accepted Answers:
a. error: func() in subDemoClass cannot override func() in superDemoClass

[Link] 6/6

Common questions

Powered by AI

The 'error: cannot assign a value to final variable' occurs because once a variable is declared as 'final', Java prohibits changing its value after initialization. This error can be resolved by ensuring that no subsequent assignments are attempted on the variable after its initial declaration, thus aligning with its intended immutability in the context of the application logic .

Static variables in Java are shared among all instances of a class, as opposed to instance variables which are unique to each object. Static variables belong to the class itself and are initialized only once when the class is loaded. Their value is common across all instances, providing a single point of reference for the variable throughout the application. This makes them ideal for constants or variables that should maintain a global state across all object instances .

In Java, methods declared as 'final' cannot be overridden by subclasses, as 'final' prevents any alterations to the method's implementation in subclasses. Attempting to override a 'final' method results in a compile-time error, indicating that the method cannot be overridden due to its 'final' status, which preserves the semantics as defined in the superclass .

The diamond problem occurs in multiple inheritance when a class inherits from two classes that both inherit from a common superclass, leading to ambiguity about the inheritance path of the superclass's methods or variables. Java avoids this issue by not supporting multiple inheritance of classes; it allows multiple interface inheritance, which does not require any implementation, thus sidestepping conflicts in method paths .

Java's exclusion of multiple class inheritance simplifies the class hierarchy, avoiding the complexity and potential conflicts that arise in languages supporting it. This leads to cleaner, more manageable code structures by compelling designers to emphasize composition over inheritance. Interfaces offer an alternative solution by allowing multiple-inheritance-like behavior without ambiguity, fostering a focus on implementing capabilities rather than tangled class dependencies .

In Java, all variables declared within an interface are implicitly 'public', 'static', and 'final'. This means they are constants that cannot change after their initial assignment. Being 'public', they can be accessed from anywhere the interface is accessible; 'static' ensures that they belong to the interface type itself rather than any instance; 'final' enforces the immutability of the values, promoting a stable and consistent interface for implementation classes .

In Java, when a class is inherited in a protected way, the public and protected members of the base class become protected members of the derived class. This access level allows the derived class to access these members even though they are not publicly accessible. Thus, protected access specifiers help maintain encapsulation by keeping the interface narrowly accessible, only to subclasses or classes in the same package .

In Java, static methods are not associated with any instance of a class, so they can be called using the class name rather than an object reference. When a static method is invoked on a null reference, the Java compiler treats it as if it were called on the class itself, rather than the null instance, preventing a NullPointerException .

In Java, a variable declared as 'final' cannot be modified after it has been initialized. This is because 'final' makes the variable a constant, enforcing immutability after initial assignment. This is a compile-time constraint, preventing reassignment and ensuring the consistency and predictability of the variable's value during runtime .

Members of a Java class are declared as 'private' to encapsulate data and restrict access to them, ensuring that they cannot be accessed directly from outside the class. This promotes data hiding and maintains the integrity of the object's state. It allows class implementers to change internal details without affecting other parts of the program that rely on the larger interface .

You might also like