Java 100 Mcqs
Java 100 Mcqs
a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once
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?
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.
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.
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();.
In the above code, emp is a reference variable. Therefore, the above object is not anonymous, as it is
assigned to a reference variable.
a. Runnable Interface
b. Marker Interface
c. Abstract Interface
d. CharSequence 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.
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.
24) Which of these classes are the direct subclasses of the Throwable 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.
a. Map m = hashMap.synchronizeMap();
b. HashMap map =hashMap.synchronizeMap();
c. Map m1 = Collections.synchronizedMap(hashMap);
d. Map m2 = Collection.synchronizeMap(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.
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
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.
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.
C.
then
D.
instanceof
class Test {
int i;
class Main {
System.out.println(t.i);
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 -
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.
Explanation: The correct sequence of HTML tags to start a webpage is html, head, title, and body.
Explanation: The anchor tag and the href attribute is used to create the link in HTML.
Explanation: To create a checkbox in HTML, we have to use the <input> tag and give the
value checkbox to its type attribute.
Explanation: The bgcolor attribute is used to set the background color of an HTML element.
a. .ht
b. .html
c. .hml
d. None of the above
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.
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
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
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;}
2) Which of the following is the correct syntax for referring the external style sheet?
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.
a. font-weight : bold
b. weight: bold
c. font: bold
d. style: 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;
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
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.
a. Inline CSS
b. Internal CSS
c. External CSS
d. None of the above
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.