0% found this document useful (0 votes)
52 views15 pages

Java 100 Mcqs

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
52 views15 pages

Java 100 Mcqs

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

5. Which of these cannot be used for a variable name in Java?

a) identifier & keyword


b) identifier
c) keyword
d) none of the mentioned
View Answer
Answer: c
Explanation: Keywords are specially reserved words that can not be used for naming a user defined variable,
for example: class, int, for, etc.

12) What will be the output of the following program?

1. public class Test {


2. public static void main(String[] args) {
3. int count = 1;
4. while (count <= 15) {
5. System.out.println(count % 2 == 1 ? "***" : "+++++");
6. ++count;
7. } // end while
8. } // end main
9. }

a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once

Hide Answer Workspace

Answer: (c) 8 times *** and 7 times +++++

Explanation: In the above code, we have declared count = 1. The value of count will be increased till 14
because of the while (count<=15) statement. If the remainder is equal to 1 on dividing the count by 2, it
will print (***) else print (+++++). Therefore, for all odd numbers till 15 (1, 3, 5, 7, 9, 11, 13, 15), it will print
(***), and for all even numbers till 14 (2, 4, 6, 8, 10, 12, 14) it will print (+++++).

18) Which of the following is true about the anonymous inner class?

a. It has only methods


b. Objects can't be created
c. It has a fixed class name
d. It has no class name
Hide Answer Workspace

Answer: (d) It has no class name

Explanation: Anonymous inner classes are the same as the local classes except that they don't have any
name. The main use of it is to override methods of classes or interfaces. And the rest three options are
false about the anonymous inner classes as it can have both methods and objects. It does not have any
fixed came name.

19) Which package contains the Random class?


a. java.util package
b. java.lang package
c. java.awt package
d. java.io package

Hide Answer Workspace

Answer: (a) java.util package

Explanation: The Random class is available in the java.util package. An object of the Random class is used
to generate a series of pseudorandom numbers. And the object of this class is a thread-safe and
cryptographically insecure object. The Random class provides a variety of methods that are used to create
random numbers of type integers, float, long, double, etc.

Hence, the correct answer is option (a).

20) What do you mean by nameless objects?

a. An object created by using the new keyword.


b. An object of a superclass created in the subclass.
c. An object without having any name but having a reference.
d. An object that has no reference.

Hide Answer Workspace

Answer: (d) An object that has no reference.

Explanation: The nameless objects are basically referred to as anonymous objects. The anonymous
objects do not have any names. We can also say that, when an object is initialized but is not assigned to
any reference variable, it is called an anonymous object. For example, new Employee();.

If we assign it to a reference variable like,


Employee emp = new Employee();

In the above code, emp is a reference variable. Therefore, the above object is not anonymous, as it is
assigned to a reference variable.

Hence, the correct answer is option (d).

21) An interface with no fields or methods is known as a ______.

a. Runnable Interface
b. Marker Interface
c. Abstract Interface
d. CharSequence Interface

Hide Answer Workspace

Answer: (b) Marker Interface

Explanation: An interface with no methods and fields is known as the marker interface. In other words, an
empty interface (containing no fields and methods) is called a marker interface. In Java, the most
commonly used marker interfaces are Serializable, Cloneable, Remote, and ThreadSafe interfaces. Marker
interfaces are also known as the Tag interface. It is used to tell the JVM or compiler that the particular
class has special behavior.

Following is the code snippet of a maker interface:

public interface Cloneable


{
// empty
}

Hence, the correct answer is option (b).

23) Which option is false about the final keyword?

a. A final method cannot be overridden in its subclasses.


b. A final class cannot be extended.
c. A final class cannot extend other classes.
d. A final method can be inherited.

Hide Answer Workspace

Answer: (c) A final class cannot extend other classes.

Explanation: The final is a reserved keyword in Java that is used to make a variable, method, and class
immutable. The important features of the final keyword are:
o Using the final keyword with a variable makes it constant or immutable. We can't reassign the
values of it.
o A final variable must be a local variable and cannot be used in other classes.
o Using the final keyword with a method makes it constant, and we can't override it in the subclass.
o Using final with a class makes the class constant, and we cannot extend a final class. But a final
class can extend other classes.

Hence, the correct answer is option (c).

24) Which of these classes are the direct subclasses of the Throwable class?

a. RuntimeException and Error class


b. Exception and VirtualMachineError class
c. Error and Exception class
d. IOException and VirtualMachineError class

Hide Answer Workspace

Answer: (c) Error and Exception class

Explanation: According to the class hierarchy of Throwable class, the Error and Exception classes are the
direct subclasses of the Throwable class, as shown below.

The RuntimeException, IOException, and VirtualMachineError classes are the subclasses of the Exception
and Error classes.

Hence, the correct answer is option (c).

37) Which of the following is a valid syntax to synchronize the HashMap?

a. Map m = hashMap.synchronizeMap();
b. HashMap map =hashMap.synchronizeMap();
c. Map m1 = Collections.synchronizedMap(hashMap);
d. Map m2 = Collection.synchronizeMap(hashMap);

Hide Answer Workspace

Answer: (c) Map m1 = Collections.synchronizedMap(hashMap);

Explanation: By default, the HashMap class is a non-synchronized collection class. The need for
synchronization is to perform thread-safe operations on the class. To synchronize the HashMap class
explicitly, we should use the Collections.synchronizedMap(hashMap) method that returns a thread-safe
map object.

Hence, the correct answer is option (c).

47) Which of the following modifiers can be used for a variable so that it can be accessed by any thread or a
part of a program?

a. global
b. transient
c. volatile
d. default

Hide Answer Workspace

Answer: (c) volatile

Explanation: In Java, we can modify the values of a variable with the help of a reserved keyword known
as volatile. It is a different way of making a class thread-safe. Thread-safe means that the methods and
objects of a class are accessible by multiple threads at the same time.

The volatile keyword is not a replacement of a synchronized block or method as it does not remove the
need for synchronization among the atomic actions.

Global is not a reserved keyword in Java. The transient and default are keywords in Java, but they are not
used for accessing a variable by a thread from any part of the program.

Hence, the correct answer is an option (c).

48) What is the result of the following program?

1. public static synchronized void main(String[] args) throws


2. InterruptedException {
3. Thread f = new Thread();
4. f.start();
5. System.out.print("A");
6. f.wait(1000);
7. System.out.print("B");
8. }

a. It prints A and B with a 1000 seconds delay between them


b. It only prints A and exits
c. It only prints B and exits
d. A will be printed, and then an exception is thrown.

Hide Answer Workspace

Answer: (d) A will be printed, and then an exception is thrown.

Explanation: The InterruptedException is thrown when a thread is waiting, sleeping, or occupied. The
output of the above code is shown below:

A
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at com.app.java.B.main(B.java:9)

In the above code, we have created a thread "f," and when started, A will be printed. After that, the thread
will wait for 1000 seconds. Now, an exception is thrown instead of printing B. It is because the wait()
method must be used inside a synchronized block or try-catch block unless it will throw an exception, as
shown above.

Hence, the correct option is option (d).

9) Which is the overloaded static method of Math class to get


absolute value in Java?
A) Math.abs(int)
B) Math.abs(float)
C) Math.abs(double)
D) All the above
Answer [=]
D
Explanation:
Notice that static methods can be overloaded in Java without any compiler error. So,
Math.abs() works out of the box with different type arguments.
3) It is not mandatory to override all or a few methods of the
Superclass. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:
Yes. It is not mandatory. If the inheriting method serves the purpose, use it directly.
Otherwise, write your custom code in the overridden method.

Q. Which of the following are not Java keywords ?


A.
double
B.
switch

C.
then
D.
instanceof

Answer : then [Option : C]

Predict the output of following Java program

class Test {

int i;

class Main {

public static void main(String args[]) {

Test t = new Test();

System.out.println(t.i);

(A) garbage value


(B) 0
(C) compiler error
(D) runtime error
Answer: (B)

Explanation: In Java, fields of classes and objects that do not have an explicit initializer and
elements of arrays are automatically initialized with the default value for their type (false for
boolean, 0 for all numerical types, null for all reference types). Local variables in Java must be
definitely assigned to before they are accessed, or it is a compile error.
24. What is the extension of compiled java classes?
a) .txt
b) .js
c) .class
d) .java
View Answer
Answer: c
Explanation: The compiled java files have .class extension.

3. Which component is used to compile, debug and execute the java programs?
a) JRE
b) JIT
c) JDK
d) JVM
View Answer
Answer: c
Explanation: JDK is a core component of Java Environment and provides all the tools, executables and
binaries required to compile, debug and execute a Java Program.
1) HTML stands for -

a. HighText Machine Language


b. HyperText and links Markup Language
c. HyperText Markup Language
d. None of these

Hide Answer Workspace

Answer: (c) HyperText Markup Language

Explanation: HTML is an acronym that stands for HyperText Markup Language, which is used for creating
web pages and web applications.

HyperText simply means "Text within Text." A text has a link within it, is a hypertext. A markup language is
a computer language that is used to apply layout and formatting conventions to a text document.

2) The correct sequence of HTML tags for starting a webpage is -

a. Head, Title, HTML, body


b. HTML, Body, Title, Head
c. HTML, Head, Title, Body
d. HTML, Head, Title, Body

Hide Answer Workspace

Answer: (d) HTML, Head, Title, Body

Explanation: The correct sequence of HTML tags to start a webpage is html, head, title, and body.

8) How to create a hyperlink in HTML?

a. <a href = "www.javatpoint.com"> javaTpoint.com </a>


b. <a url = "www.javatpoint.com" javaTpoint.com /a>
c. <a link = "www.javatpoint.com"> javaTpoint.com </a>
d. <a> www.javatpoint.com <javaTpoint.com /a>

Hide Answer Workspace

Answer: (a) <a href = "www.javatpoint.com"> javaTpoint.com </a>

Explanation: The anchor tag and the href attribute is used to create the link in HTML.

15) How to create a checkbox in HTML?


a. <input type = "checkbox">
b. <input type = "button">
c. <checkbox>
d. <input type = "check">

Hide Answer Workspace

Answer: (a) <input type = "checkbox">

Explanation: To create a checkbox in HTML, we have to use the <input> tag and give the
value checkbox to its type attribute.

12) How to add a background color in HTML?


a. <marquee bg color: "red">
b. <marquee bg-color = "red">
c. <marquee bgcolor = "red">
d. <marquee color = "red">

Hide Answer Workspace

Answer: (c) <marquee bgcolor = "red">

Explanation: The bgcolor attribute is used to set the background color of an HTML element.

31) An HTML program is saved by using the ____ extension.

a. .ht
b. .html
c. .hml
d. None of the above

Hide Answer Workspace

Answer: (b) .html

Explanation: The .html or .htm extension both are used to save the HTML program. We can save our
HTML file either by using the .html extension or by .htm extension. When the file delivered to the
network, the HTML extension indicates that the content of file is HTML.

17. Which element is used for or styling HTML5 layout?


a) CSS
b) jQuery
c) JavaScript
d) PHP
View Answer
Answer: a
Explanation: For styling HTML5, CSS i.e Cascading Style Sheet is used. It is style sheet language and
designed to describe presentation of its content including layouts, colors and fonts. CSS can control the layout
26. Which of the following tag is used to create an unordered list?
a) <ol>
b) <ul>
c) <li>
d) <ll>
View Answer
Answer: b
Explanation: <ul> tag is used to create the unordered list items in an HTML document. By default, unordered
list items will display in a bulleted format.
Different types of unordered list Attribute Values are:

19. Which HTML tag is used to insert an image?


a) <img url=”htmllogo.jpg” />
b) <img alt=”htmllogo.jpg” />
c) <img src=”htmllogo.jpg” />
d) <img link=”htmllogo.jpg” />
View Answer

Answer: c
Explanation: In HTML, <img> tag is used to insert an image in HTML page. Image tag has two attributes (src
and Alt).
–> Src attribute is used to specify the image path.
–> Alt attribute is used to specify the alternate text for the image

32. What is the correct syntax of web address?


a) port://domain.filenmae:path/scheme/prefix
b) prefix://scheme.port:domain/filename/path
c) path://prefix.port:domain/filename/scheme
d) scheme://prefix.domain:port/path/filename
View Answer
Answer: d
Explanation: The correct syntax for a web address is scheme://prefix.domain:port/path/filename, where scheme
is for https or http, prefix is for domain like www, domain denotes domain name, port defines port number, path
defines path at server, filename is for name of the document.

48. Which HTML element is used for YouTube videos?


a) <samp>
b) <small>
c) <frame>
d) <iframe>
View Answer
Answer: d
Explanation: We can host our videos through YouTube and can easily embed them into our website. <iframe>
tag will be used for YouTube videos.
E.g. <iframe width=”670” height=”612” src= “https://sanfoundry.com”> </iframe>

1. How many standard color names does HTML supports?


a) 120
b) 130
c) 140
d) 90
View Answer
Answer: c
Explanation: In HTML one can use a color name to specify a color e.g. Tomato, Orange, DodgerBlue,
MediumSeaGreen, Gray, SlateBlue, Violet, LightGray etc. For colored fonts, we used text-decoration property
to set the color of the text.
3. What should be set with text-align property so that every line has equal width like in magazines and
newspapers?
a) text-align: justify
b) text-align: none
c) text-align: bottom
d) text-align: top
View Answer
Answer: a
Explanation: By setting text-align property to “justify”, each line is stretched so that every line has equal width,
left and right margins are straight like in magazines and newspapers. Text-align: center, text-align: right, text-
align: left is also used for aligning the text at center, right and left respectively.

33) Which of the following CSS property specifies the origin of the background-image?

a. background-origin
b. background-attachment
c. background-size
d. None of the above

Hide Answer Workspace

Answer: (a) background-origin

Q. By default Hyperlinks are displayed with an underline. How do you remove the underline from
all hyperlinks by using CSS code ?
A.
a {text: no-underline;}
B.
a {text-decoration:none;}

C.
a {text-style: no-underline;}
D.
a {text-decoration: no-underline;}

Answer : a {text-decoration:none;} [Option : B]

Q. Which CSS property is used to control the text size of an element ?


A.
font-style
B.
text-size
C.
font-size
D.
text-style

Answer : font-size [Option : C]

2) Which of the following is the correct syntax for referring the external style sheet?

a. <style src = example.css>


b. <style src = "example.css" >
c. <stylesheet> example.css </stylesheet>
d. <link rel="stylesheet" type="text/css" href="example.css">

Hide Answer Workspace

Answer: (d) <link rel="stylesheet" type="text/css" href="example.css">

Explanation: The external style sheet is generally used when you want to make changes on multiple
pages. It uses the <link> tag on every pages and the <link> tag should be put inside the head section.

11.The CSS property used to make the text bold is -

a. font-weight : bold
b. weight: bold
c. font: bold
d. style: bold

Hide Answer Workspace

Answer: (a) font-weight : bold

Explanation: The font-weight property is used for setting the thickness and boldness of the font. It is
used to define the weight of the text. The available weight depends on the font-family, which is used by
the browser.

19) Which of the following syntax is correct in CSS to make each word of a sentence start with a capital letter?
a. text-style : capital;
b. transform : capitalize;
c. text-transform : capital;
d. text-transform : capitalize;

Hide Answer Workspace

Answer: (d) text-transform : capitalize;

Explanation: The text-transform CSS property allows us to change the case of the text. It is used to
control text capitalization. The "capitalize" value of the text-transform property transforms the first
character of each word to uppercase. It will not capitalize the first letter after the number.

26) Which of the following CSS property is used to specify whether the table cells share the common or
separate border?

a. border-collapse
b. border-radius
c. border-spacing
d. None of the above

Hide Answer Workspace

Answer: (a) border-collapse

Explanation: The border-collapse CSS property is used to set the border of the table cells and specifies
whether the table cells share a separate or common border. This property has two main values that are
separate and collapse.

32) Which type of CSS is used in the below code?

1. <p style = "border:2px solid red;">

a. Inline CSS
b. Internal CSS
c. External CSS
d. None of the above

Hide Answer Workspace

Answer: (a) Inline CSS

Explanation: If you want to use inline CSS, you should use the style attribute to the relevant tag. The
inline CSS is also a method to insert style sheets in HTML document.

You might also like