0% found this document useful (0 votes)
8 views27 pages

Java

The document contains a series of multiple-choice questions (MCQs) related to Java applets and constructors, along with their correct answers. It covers topics such as applet functionality, graphics, HTML integration, and constructor behavior in Java. The document also includes explanations for certain code snippets demonstrating the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

Java

The document contains a series of multiple-choice questions (MCQs) related to Java applets and constructors, along with their correct answers. It covers topics such as applet functionality, graphics, HTML integration, and constructor behavior in Java. The document also includes explanations for certain code snippets demonstrating the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

What is a Java Applet?


A. An operating system
B. A small Java program that runs inside a browser
C. A database tool
D. A compiler
✅ Answer: B

2.

Which of the following is required to run an applet?


A. Notepad
B. Web browser or Applet Viewer
C. Excel sheet
D. Command prompt only
✅ Answer: B

3.

Which package must be imported to create an applet?


A. [Link]
B. [Link]
C. [Link]
D. Both B and C
✅ Answer: D

4.

The Graphics class is used for:


A. File handling
B. Drawing shapes and text
C. Database connection
D. Internet communication
✅ Answer: B

5.

Which method is used to display text on an applet window?


A. print()
B. drawText()
C. drawString()
D. displayText()
✅ Answer: C

6.

What is the purpose of the paint(Graphics g) method?


A. To start the applet
B. To draw and display content on the applet window
C. To stop the applet
D. To load images
✅ Answer: B

7.

What is the file extension of a Java applet source file?


A. .html
B. .java
C. .class
D. .exe
✅ Answer: B

8.

After compilation of a Java applet, which file is generated?


A. .exe
B. .txt
C. .class
D. .html
✅ Answer: C

9.

Which HTML tag is used to embed a Java applet in a web page?


A. <java>
B. <embed>
C. <applet>
D. <object>
✅ Answer: C

10.

In an HTML file, which attribute specifies the name of the compiled applet file?
A. name
B. code
C. src
D. class
✅ Answer: B

11.

In the <applet> tag, what do the attributes width and height define?
A. Font size of text
B. Screen resolution
C. Display area of the applet
D. Size of the HTML page
✅ Answer: C

12.

Which tool is used to run a Java applet?


A. Java Runtime Environment
B. Applet Viewer
C. NetBeans IDE
D. JavaFX Runner
✅ Answer: B

13.

Which file is executed using the appletviewer command?


A. .class file
B. .java file
C. .html file
D. .txt file
✅ Answer: C
14.

What happens if you add another [Link]() in the paint method?


A. The applet will crash
B. The applet will display both text strings
C. Only the last text will appear
D. Compilation error occurs
✅ Answer: B

15.

Changing the width and height attributes in the HTML file affects what?
A. Background color
B. Font size
C. Size of the applet display area
D. Text position only
✅ Answer: C

16.

In [Link]("Hello", 150, 150);, what does 150, 150 represent?


A. Text font size
B. Window size
C. X and Y position of the text
D. Screen resolution
✅ Answer: C

17.

Which coordinate system does Java applet graphics use?


A. Cartesian (origin at center)
B. Cartesian (origin at bottom-left)
C. Pixel-based (origin at top-left)
D. Polar coordinates
✅ Answer: C

18.

What must be done after modifying a Java applet’s source code?


A. Restart the computer
B. Recompile the Java file
C. Edit the HTML file
D. Nothing
✅ Answer: B

19.

If only the HTML file is changed (not the Java code), what should be done?
A. Recompile again
B. Reinstall JDK
C. No recompilation needed
D. Delete class file
✅ Answer: C

20.

Which of the following statements about applets is TRUE?


A. Applets can run directly from the command prompt
B. Applets must extend the Applet class
C. Applets cannot use the AWT package
D. Applets do not require compilation
✅ Answer: B

Would you like me to create a PDF file of these 20 MCQs (with clean formatting and space
for writin

Java Applet – 20 MCQs with Answers

1.

What is a Java Applet?


A. An operating system
B. A small Java program that runs inside a browser
C. A database tool
D. A compiler
✅ Answer: B

2.
Which of the following is required to run an applet?
A. Notepad
B. Web browser or Applet Viewer
C. Excel sheet
D. Command prompt only
✅ Answer: B

3.

Which package must be imported to create an applet?


A. [Link]
B. [Link]
C. [Link]
D. Both B and C
✅ Answer: D

4.

The Graphics class is used for:


A. File handling
B. Drawing shapes and text
C. Database connection
D. Internet communication
✅ Answer: B

5.

Which method is used to display text on an applet window?


A. print()
B. drawText()
C. drawString()
D. displayText()
✅ Answer: C

6.

What is the purpose of the paint(Graphics g) method?


A. To start the applet
B. To draw and display content on the applet window
C. To stop the applet
D. To load images
✅ Answer: B

7.

What is the file extension of a Java applet source file?


A. .html
B. .java
C. .class
D. .exe
✅ Answer: B

8.

After compilation of a Java applet, which file is generated?


A. .exe
B. .txt
C. .class
D. .html
✅ Answer: C

9.

Which HTML tag is used to embed a Java applet in a web page?


A. <java>
B. <embed>
C. <applet>
D. <object>
✅ Answer: C

10.

In an HTML file, which attribute specifies the name of the compiled applet file?
A. name
B. code
C. src
D. class
✅ Answer: B
11.

In the <applet> tag, what do the attributes width and height define?
A. Font size of text
B. Screen resolution
C. Display area of the applet
D. Size of the HTML page
✅ Answer: C

12.

Which tool is used to run a Java applet?


A. Java Runtime Environment
B. Applet Viewer
C. NetBeans IDE
D. JavaFX Runner
✅ Answer: B

13.

Which file is executed using the appletviewer command?


A. .class file
B. .java file
C. .html file
D. .txt file
✅ Answer: C

14.

What happens if you add another [Link]() in the paint method?


A. The applet will crash
B. The applet will display both text strings
C. Only the last text will appear
D. Compilation error occurs
✅ Answer: B

15.
Changing the width and height attributes in the HTML file affects what?
A. Background color
B. Font size
C. Size of the applet display area
D. Text position only
✅ Answer: C

16.

In [Link]("Hello", 150, 150);, what does 150, 150 represent?


A. Text font size
B. Window size
C. X and Y position of the text
D. Screen resolution
✅ Answer: C

17.

Which coordinate system does Java applet graphics use?


A. Cartesian (origin at center)
B. Cartesian (origin at bottom-left)
C. Pixel-based (origin at top-left)
D. Polar coordinates
✅ Answer: C

18.

What must be done after modifying a Java applet’s source code?


A. Restart the computer
B. Recompile the Java file
C. Edit the HTML file
D. Nothing
✅ Answer: B

19.

If only the HTML file is changed (not the Java code), what should be done?
A. Recompile again
B. Reinstall JDK
C. No recompilation needed
D. Delete class file
✅ Answer: C

20.

Which of the following statements about applets is TRUE?


A. Applets can run directly from the command prompt
B. Applets must extend the Applet class
C. Applets cannot use the AWT package
D. Applets do not require compilation
✅ Answer: B

Would you like me to create a PDF file of these 20 MCQs (with clean formatting and space
for writing answers) so you can keep it as study or submission material?

20 MCQs (in English)

1. Which method in an applet is used for initialization?


A. start() B. paint() C. init() D. main()
✅ Answer: C

2. What is the function of setBackground([Link]);?


A. Sets text color B. Sets background color C. Resizes window D. Prints message
✅ Answer: B

3. Which method changes the applet window size?


A. resize() B. repaint() C. start() D. init()
✅ Answer: A

4. Which class contains predefined colors like red, blue, and yellow?
A. String B. Color C. Graphics D. Applet
✅ Answer: B

5. Applets cannot take input directly from _______.


A. HTML B. Keyboard C. File D. Both B and C
✅ Answer: D

6. Which HTML tag passes data to an applet?


A. <code> B. <input> C. <param> D. <value>
✅ Answer: C
7. The getParameter() method returns data of type ______.
A. int B. char C. String D. double
✅ Answer: C

8. Which method is used to convert a string to an integer?


A. parseInt() B. getInt() C. toInt() D. convert()
✅ Answer: A

9. The drawString() method belongs to which class?


A. Applet B. Color C. Graphics D. String
✅ Answer: C

10. If we modify only the HTML file, do we need to recompile the applet?
A. Yes B. No
✅ Answer: B

11. Applet output appears on ______.


A. Console B. Browser/Applet viewer C. Notepad D. Terminal
✅ Answer: B

12. What happens if the HTML file name is wrong while running appletviewer?
A. Compiles B. Runs normally C. Fails to execute D. Displays blank screen
✅ Answer: C

13. Which tag in HTML specifies the applet class name?


A. <param> B. <applet> C. <body> D. <object>
✅ Answer: B

14. Can HTML file and class file have different names?
A. Yes B. No
✅ Answer: A

15. The [Link]() method is used for ______.


A. String conversion B. Integer parsing C. Character splitting D. Float conversion
✅ Answer: B

16. What does appletviewer interpret?


A. Java bytecode B. HTML code C. XML data D. JSON file
✅ Answer: B

17. Applet programs are stored in which file format after compilation?
A. .java B. .html C. .class D. .exe
✅ Answer: C

18. Where should the <param> tag be written?


A. Inside <body> B. Inside <applet> C. Outside <html> D. Anywhere
✅ Answer: B
19. Can an applet display input without HTML file?
A. Yes, if written inside same file comment B. No
✅ Answer: A

20. Best practice in applet programming is to keep ______.


A. Java and HTML in one file B. Separate Java and HTML files
✅ Answer: B

21. Which statement is true about constructors?


a) They can have return types
b) They are called manually
c) They are automatically called when object is created
d) They must be static
Ans: c
22. What will be the output?
23. class A {
24. A() { this(10); [Link]("A()"); }
25. A(int x) { [Link]("A(int): " + x); }
26. }
27. new A();

a) A() A(int):10
b) A(int):10 A()
c) Error
d) A(int):10
Ans: b

28. The keyword this() is used for —


a) Calling superclass constructor
b) Calling another constructor in the same class
c) Calling any method
d) Referring to static members
Ans: b
29. Which of these is a valid overloaded constructor?
a) A(int x) and A(int y)
b) A() and A(int)
c) A(int) and A(int)
Ans: b
30. Can a constructor call itself directly using this()?
a) Yes
b) No, it causes recursion error
c) Only once
d) Only if static
Ans: b
31. What will be printed?
32. class B {
33. B() { [Link]("One"); }
34. B(int x) { this(); [Link]("Two"); }
35. }
36. new B(5);
a) One Two
b) Two One
c) Error
Ans: a

37. What is the purpose of constructor chaining?


a) To create multiple objects
b) To call one constructor from another
c) To destroy object
Ans: b
38. this() must be —
a) The last line in constructor
b) First line in constructor
c) Optional line
Ans: b
39. Which keyword is used to call parent class constructor?
a) this
b) super
c) parent
Ans: b
40. Which constructor is called if no arguments are passed?
a) Default
b) Parameterized
Ans: a
41. What happens if a class has no constructor?
a) Error
b) Java compiler adds a default one
Ans: b
42. Can constructors be overloaded?
a) Yes
b) No
Ans: a
43. In constructor chaining, the order of execution is —
a) Child first
b) Parent first
c) Random
Ans: b
44. Can we use this() and super() in the same constructor?
a) Yes
b) No
Ans: b
45. What is the output?
46. class Test {
47. Test() { this(5); [Link]("Hi"); }
48. Test(int x) { [Link]("Hello " + x); }
49. }
50. new Test();

a) Hello 5 Hi
b) Hi Hello 5
Ans: a
51. Which of the following is false?
a) Constructors can be private
b) Constructors have return types
Ans: b
52. Constructor chaining can happen —
a) Within same class using this()
b) Across classes using super()
c) Both
Ans: c
53. If both this() and super() are missing, what happens?
a) Compiler adds super() automatically
Ans: a
54. Which of these statements about this() is false?
a) It must be first line
b) It can call multiple constructors
c) It calls only one constructor
Ans: b
55. Which best defines constructor overloading?
a) Multiple constructors with same parameters
b) Multiple constructors with different parameters
Ans: b

🧠 1.

class A {

A() { [Link]("A created"); }

class B extends A {

B() { [Link]("B created"); }

public class Test {

public static void main(String[] args) {

new B();

Output:

A created

B created
Explanation:
Superclass constructor (A()) is always called first automatically before subclass constructor
(B()).

🧠 2.

class Animal {

Animal() { [Link]("Animal"); }

class Dog extends Animal {

Dog() { super(); [Link]("Dog"); }

class Puppy extends Dog {

Puppy() { [Link]("Puppy"); }

public class Test {

public static void main(String[] args) {

new Puppy();

Output:

Animal

Dog

Puppy

Explanation:
Constructor chaining: superclass → subclass → sub-subclass.

🧩 3.

class A {

A(int x) { [Link]("A with " + x); }


}

class B extends A {

B() { super(5); [Link]("B"); }

public class Test {

public static void main(String[] args) {

new B();

Output:

A with 5

Explanation:
super(5) explicitly calls the parameterized constructor of A.

🧩 4.

class A {

A() { [Link]("A"); }

class B extends A {

B() { [Link]("B"); }

class C extends B {

C() { super(); [Link]("C"); }

public class Test {

public static void main(String[] args) {

new C();
}

Output:

💡 5.

class Animal {

void sound() { [Link]("Animal sound"); }

class Dog extends Animal {

void sound() { [Link]("Bark"); }

public class Test {

public static void main(String[] args) {

Animal a = new Dog();

[Link]();

Output:

Bark

Explanation:
Runtime polymorphism → Dog's method overrides Animal's method.

💡 6.

class Bike {

void run() { [Link]("Bike running"); }


}

class Splendor extends Bike {

void run() { [Link]("Splendor running fast"); }

public class Test {

public static void main(String[] args) {

Bike b = new Splendor();

[Link]();

Output:

Splendor running fast

🔥 7.

class A {

static void show() { [Link]("Static A"); }

class B extends A {

static void show() { [Link]("Static B"); }

public class Test {

public static void main(String[] args) {

A a = new B();

[Link]();

Output:

Static A
Explanation:
Static methods are not overridden, only hidden. So method call is resolved at compile time
by reference type (A).

🔥 8.

class Parent {

void show() { [Link]("Parent"); }

class Child extends Parent {

void show() { [Link]("Child"); }

public class Test {

public static void main(String[] args) {

Parent p = new Child();

((Child)p).show();

Output:

Child

Explanation:
Type casting doesn’t change the behavior; it’s still runtime polymorphism.

⚡ 9.

class A {

int x = 10;

class B extends A {

int x = 20;

}
public class Test {

public static void main(String[] args) {

A obj = new B();

[Link](obj.x);

Output:

10

Explanation:
Variables are not polymorphic — compile-time type (A) decides which variable is accessed.

⚡ 10.

class A {

void display() { [Link]("A"); }

class B extends A {

void display() { [Link]("B"); }

public class Test {

public static void main(String[] args) {

A obj = new B();

[Link]();

Output:

Explanation:
Method overriding — runtime binding.
🧠 11.

abstract class Shape {

abstract void draw();

class Circle extends Shape {

void draw() { [Link]("Circle drawn"); }

public class Test {

public static void main(String[] args) {

Shape s = new Circle();

[Link]();

Output:

Circle drawn

🧠 12.

abstract class A {

abstract void show();

class B extends A {}

public class Test {

public static void main(String[] args) {

// new A(); ❌

// new B(); ❌

}
Compilation Error:
Class B must implement abstract method show() or be declared abstract.

🧩 13.

final class A {}

class B extends A {}

Output:
❌ Compilation Error
Explanation:
final class cannot be extended.

🧩 14.

class A {

final void display() { [Link]("A"); }

class B extends A {

void display() { [Link]("B"); }

Output:
❌ Compilation Error
Cannot override final method.

🧠 15.

class A {

A() { [Link]("A"); }

class B extends A {

B(int x) { [Link]("B" + x); }

public class Test {


public static void main(String[] args) {

new B(5);

Output:

B5

⚡ 16.

class A {

A() { [Link]("A"); }

class B extends A {

B() { super(); [Link]("B"); }

class C extends B {

C() { [Link]("C"); }

public class Test {

public static void main(String[] args) {

new C();

Output:

C
🔥 17.

class A {

A() { [Link]("A"); }

class B extends A {

B() { [Link]("B"); }

class C extends B {

C() { super(); [Link]("C"); }

class D extends C {

D() { [Link]("D"); }

public class Test {

public static void main(String[] args) {

new D();

Output:

💥 18.

class Parent {

Parent(int x) { [Link]("Parent: " + x); }

}
class Child extends Parent {

Child() { super(10); [Link]("Child"); }

public class Test {

public static void main(String[] args) {

new Child();

Output:

Parent: 10

Child

💥 19.

abstract class Vehicle {

abstract void run();

class Car extends Vehicle {

void run() { [Link]("Car running"); }

class Bike extends Vehicle {

void run() { [Link]("Bike running"); }

public class Test {

public static void main(String[] args) {

Vehicle v = new Bike();

[Link]();

}
Output:

Bike running

💣 20.

class A {

static { [Link]("Static A"); }

A() { [Link]("A constructor"); }

class B extends A {

static { [Link]("Static B"); }

B() { [Link]("B constructor"); }

public class Test {

public static void main(String[] args) {

new B();

Output:

Static A

Static B

A constructor

B constructor

Explanation:
Static blocks run once at class loading time, constructors run every time an object is
created.

Would you like me to now make a PDF of these 20 tricky coding questions (with outputs and
explanations) so you can use it for your notes or tests?

✅ Key Concepts to Remember


1. package → to create a package.

2. import → to use a package/class in another program.

3. Directory structure must match the package name.

4. Use * in import if you want all classes from a package.

5. Organize code in packages to avoid clutter and confusion.

If you want, I can draw a small diagram showing how working directory → package → class
→ import works. It will make this much easier

You might also like