0% found this document useful (0 votes)
4K views8 pages

Java Quiz Attempt Review

The summary provides the key details from the document: 1) The document is a review of a quiz that was taken on Java programming topics. 2) The quiz contained 8 questions and was completed in 8 minutes and 11 seconds. 3) The participant received a perfect score of 8 out of 8 and a grade of 100% on the quiz.

Uploaded by

19K41A0 450
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)
4K views8 pages

Java Quiz Attempt Review

The summary provides the key details from the document: 1) The document is a review of a quiz that was taken on Java programming topics. 2) The quiz contained 8 questions and was completed in 8 minutes and 11 seconds. 3) The participant received a perfect score of 8 out of 8 and a grade of 100% on the quiz.

Uploaded by

19K41A0 450
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
  • Quiz Overview
  • Question 1
  • Question 2 and 3
  • Question 4
  • Question 5
  • Question 6
  • Question 7
  • Question 8

11/14/23, 3:58 PM Post-Quiz: Attempt review


 Dashboard / App Dev / Stage - 1 / Java Programming / Classes and Objects, Packages

Quiz review
Started on Sunday, 12 November 2023, 6:55 PM
State Finished
Completed on Sunday, 12 November 2023, 7:03 PM
Time taken 8 mins 11 secs
Marks 8.00/8.00
Grade 100.00 out of 100.00
Feedback Congratulations!! You have passed by securing more than 80%

42763

42763

42763

[Link] 1/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 1
Correct

Mark 1.00 out of 1.00

Given:

public class Message {


String msg;
int noOfWords;

public Message() {
msg += " Thank you";
}
public Message(int noOfWords) {

[Link] = noOfWords;
msg = "Welcome";

42763
Message();

}
public static void main(String args[]) {

Message m = new Message(5);

[Link]([Link]);
}

What will be the output ?

Select one:
a. Welcome Thank you 5 42763
b. An exception is thrown at runtime

c. Compilation fails

d. Welcome Thank you

e. Welcome

f. The code runs with no output

Your answer is correct.

The correct answer is: Compilation fails 42763

[Link] 2/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 2
Correct

Mark 1.00 out of 1.00

Integer x1 = new Integer(120);

int x2 = 120;
[Link]( x1 == x2 );

What will be the output of the above code fragment?

Select one:
a.
Runtime Exception

b. Compilation Error

c.
CastException

42763
d. true

e. false

Your answer is correct.

The correct answer is: true

Question 3
Correct

Mark 1.00 out of 1.00

42763
Which of the following is not a Java modifier?

Select one:
a.
public

b. 
virtual

c. private

d.

42763
protected

Your answer is correct.

The correct answer is: virtual

[Link] 3/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 4
Correct

Mark 1.00 out of 1.00

The methods of a class with the ____________ access specifier cannot be accessed in its subclass class of different package.

Select one:
a. default

b. protected

c. public

Your answer is correct.


We call the access specifier default as “package level access” because, in a clss, members declared as default can be accessed only
within the package in which it is declared.

42763
The correct answer is: default

42763

42763

[Link] 4/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 5
Correct

Mark 1.00 out of 1.00

Observe the code

public class Sample {


public static void main(String [] args) {

int x = 6;
Sample p = new Sample();
[Link](x);
[Link](" main x = " + x);

}
void display(int x) {

42763
[Link](" display x = " + x++);

}
}

Given in command line - java Sample - What is the result?

Select one:
a.
display x = 7 main x = 6

b.
display x = 6 main x = 7

42763
c.
Compilation fails.

d. 
display x = 6 main x = 6

e. An exception is thrown at runtime.

f.
display x = 7 main x = 7

Your answer is correct.

The correct answer is:


display x = 6 main x = 6

42763

[Link] 5/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 6
Correct

Mark 1.00 out of 1.00

What is the outcome of the code?

public class Item {


private String description;

public String getDescription() {


return description;
}
public void setDescription(String description) {

[Link] = description;

42763
public static void modifyDesc(Item item,String desc) {

item=new Item();
[Link](desc);

public static void main(String[] args) {


Item it=new Item();

[Link]("Gobstopper");
Item it2=new Item();

[Link]("Fizzylifting");

42763
[Link](it,"Scrumdiddlyumptious");
[Link]([Link]());

[Link]([Link]());

}
}

Select one:
a. Gobstopper

Scrumdiddlyumptious

b. Scrumdiddlyumptious

Fizzylifting

c. Compilation fails
42763
d. Gobstopper

Fizzylifting

e. Scrumdiddlyumptious

Your answer is correct.

The correct answer is: Gobstopper


Fizzylifting

[Link] 6/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 7
Correct

Mark 1.00 out of 1.00

Predict the output.

class X
{
void display(int a)

{
[Link]("INT");
}

void display(double d)
{

42763
[Link]("DOUBLE");

}
}

public class Sample


{

public static void main(String[] args)


{

new X().display(100);

42763
}
}

Select one:
a. INT

b. Ambiguity error

c. Compilation Fails

d. DOUBLE

Your answer is correct.

The correct answer is: INT


42763

[Link] 7/8
11/14/23, 3:58 PM Post-Quiz: Attempt review

Question 8
Correct

Mark 1.00 out of 1.00

Given classes defined in two different files:

1. package p1;
2. public class Test {

3. public static void display(String [] a) { /* some code */ }


4. }

1. package p2;

2. public class TestMain {


3. public static void main(String[] args) {

42763
4. String [] names = new String[10];

5. // insert code here


6. }

7. }

Identify the statement to be written in line 5 in class TestMain to call the display method of class Test.

Select one:
a.
display(names);

b.

c.
[Link](names);

42763
import [Link].*; display(names);

d. [Link](names);

e.
TestMain cannot use methods in p1

Your answer is correct.

The correct answer is: [Link](names);

42763
◄ Incredible Toys

Jump to...

Additional Learning ►

[Link] 8/8

Common questions

Powered by AI

A compilation error occurs when a method is mistaken for a constructor, as constructors do not have return types. The attempt to call 'Message()' as a method-like function within another constructor confuses the compiler, which expects 'this()' or 'super()' to be used for invoking constructors. This leads to a syntax error preventing program execution .

Autoboxing allows an Integer object to be compared to an int by automatically converting the Integer object to its primitive int value. Therefore, 'x1 == x2' evaluates to true because x1 is unboxed to an int with value 120, making the comparison with x2 valid and correct .

A call to 'new Item()' within a static method such as 'modifyDesc' does not affect the original object because Java uses pass-by-value for object references. Thus, when a new 'Item' is instantiated, the reference in the method points to a new memory location, leaving the original object outside the method unchanged. The changes are applied only to the new instance created within the method scope .

The 'Item' class demonstrates that object attributes remain unchanged when a method manipulates an object reference passed to it. In 'modifyDesc', the object reference 'item' is reassigned to a new 'Item' object within the method, so any modifications apply only to this new instance. The attributes of the original object remain unchanged as the reassignment affects only the method's local scope .

To access a method from a class in a different package, you must specify the package name as a prefix unless the class is imported. Therefore, to call 'display' from class 'Test' in package 'p1', the correct method is 'p1.Test.display(names)', since the method is static, allowing access via the class name directly .

The constructor call 'Message()' within another constructor in the Message class is incorrect because Java does not allow a direct invocation of a constructor like a method. Instead, the correct way is to call a constructor using 'this()' or 'super()'. The code as written attempts to call itself in a non-static manner, leading to a compilation error .

In Java, variables are passed by value, meaning a copy of the variable is passed to the method. In the 'Sample' class, when 'display' is called, 'x' represents a separate copy of the value. Modifying 'x' inside the method does not affect the original variable in 'main'. Therefore, 'main x' remains unchanged and prints its original value, even if 'display x' is incremented .

The 'default' access modifier, also known as package-private, restricts access to the members to within the same package. In contrast, 'protected' provides package-level access and in addition allows subclasses outside the package to access the members. Thus, default methods cannot be accessed in subclasses if the subclass is in a different package, unlike protected methods .

The method 'modifyDesc' does not change the description of the passed 'Item' object because it instantiates a new 'Item' object within the method, which only modifies the newly created object's description. The original 'Item' object remains unaffected as the variable 'item' in 'modifyDesc' points to a new object instance. Hence, the changes are not reflected in the original 'Item' object .

In Java, method overloading relies on the parameter types specified in the method signatures to resolve which method to invoke. In the 'X' class, when 'display(100)' is called, Java selects 'display(int a)' over 'display(double d)' because 100 is an integer literal, which matches directly with the int parameter type .

You might also like