Java Practice Questions ,Class-XII
JAVA
PRACTICE QUESTIONS:
1.
Main Method is a special method that every Java
application must have.
2.
DO-WHILE loop run at least once.
3.
Once
the class is defined, we can create OBJECT
of the class and access its members.
4.
A
Package java is a group of related classes.
5.
Predict the output
{
double[]Qty = {46, 42, 13, 44, 87.5};
System.out.println(Qty[3]);
}
44
(because it start from 0 then 1 then 2 accordingly at 3 is “44”)
6.
Which component is used to compile, debug
and execute java program?
JDK(Java
Development Kit)
7.
Which of the following is an invalid
variable declaration?
a) my_string_1 b) 1st_string c) mystring1 d) _mystring1
b) ParseInt
() method takes a
String as parameter and returns the equivalent integer.
8.
Java is Case Sensitive Language.
Explain
Yes,
Java is a case-sensitive programming language. This means that the compiler
treats uppercase and lowercase letters differently. This can be a source of
confusion for some beginners, but it is an important concept to understand.
Here are some examples to illustrate the point:
·
Variable names: age and Age are considered two
different variables in Java.
·
Keywords: public and Public are not the same
keyword.
·
Method names: print () and Print () are
different methods.
·
Class names: Car and car refer to two separate
classes.
9.
Find error and rewrite the correct
code
i == 0;
while (i < 5)
{
System.out.println(i) i++;
Correct code: 4 errors
i =0;
while (i < 5)
{
System.out.println(i);
i++;
}
10.
Complete the code in JAVA using a
loop to print "Yes" 10 times:
______ (int i = 0; i < 5; _______
) { ______(”Yes”); }
Complete code in JAVA
For(int i=0;i<10;i++)
{
System.out.Println(“Yes”);
}
11.
Write a single line Java code using String methods to perform the
following tasks
String
cl = ‘‘class xii’’;
i.
Convert the characters to capital letters.
ii.
Find the total length of string.
iii.
Add a word ‘‘toppers” at the
end of original string.
iv.
What is the purpose of a
constructor?
i.
cl.toUpperCase( )
ii.
cl.length();
iii.
cl.concat(”toppers”);
iv.
The constructor is a method used to initialize
the data members of the class.
12.
Name the user defined method in the
code given below. Also predict the output
static int myMethod(int x)
{ return ( 5 + x); }
public static void main(String[] args) {
System.out.println(myMethod(3)); }
1.
User Defined Method: The name of the
user-defined method in the code is myMethod.
2.
Output Prediction: The code calls the myMethod
with a value of 3. The myMethod adds 5 to the passed value and returns the
result. Therefore, the output will be:8
13.
Explain any two-access modifiers.
Two common access modifiers in Java:
Private:
·
Access
level: Accessible only within the class where it is defined.
·
Use
case: For methods and fields that should not be directly accessed by other
classes and maintain encapsulation.
Public:
·
Access
level: Accessible from anywhere in the program.
·
Use
case: For methods and fields that need to be accessed by other classes.
14.
What is an exception? b. Explain
each of the following terms in exception handling i. try ii. catch iii. Finally
a)
It is an event that disrupts the normal flow of program execution.
It can be caused by various factors, such as:
·
Runtime
errors: These errors occur
during program execution, such as dividing by zero or accessing an
out-of-bounds array element.
·
Checked
exceptions: These are
exceptions that are checked by the compiler and require explicit
handling. Examples include IOException
and ClassNotFoundException
.
·
Unchecked
exceptions: These are
exceptions that are not checked by the compiler and do not require explicit
handling. Examples include ArithmeticException
and NullPointerException
.
b)
Exception Handling Terms:
1.
try:
The try
block is used to
specify the code that may throw an exception. If an exception occurs within the
try
block, the
program execution is transferred to the appropriate catch
block.
2.
catch: The catch
block is used to handle the exception thrown by the try
block. Each catch
block can specify a specific type of exception it can handle. If an exception
is thrown within the try
block, the program execution is transferred to the first catch
block that can handle the exception type.
3.
finally: The finally
block is used to execute code regardless of whether an exception occurs. This
can be helpful for cleanup tasks, such as closing files or releasing resources.
The finally
block is
always executed after the try
and catch
blocks, even
if an exception is thrown.
15.
Give the output of the following
code segment
public class Main
{
public static void main(String[] args)
{
int day = 4;
switch (day)
{
case 6: System.out.println("Today is
Saturday");
break;
case 7: System.out.println("Today is
Sunday");
break;
default: System.out.println("Looking
forward to the Party");
}
}
}
Ans: Looking forward to the Party
16.
What is a byte code and JVM?
Byte code is set of instructions or Java class
file or Bytecode is an intermediate representation of a
program that is designed to be executed by a virtual machine.
The Java Virtual Machine (JVM) is a software program that
provides a runtime environment for Java programs
17.
Name one front end and one Back-end
application to create Web Applications.
Front end Java NetBeans Back end MySQL
18.
Write the types of comment available
in JAVA.
Two types of
comment are as follows
1.
Single-line Comments:
·
Syntax: Start with two forward
slashes (//)
·
Example: This is a single-line comment.
2.
Multi-line Comments:
·
Syntax: Begin with /* and end with
*/
·
Example: This is a multi-line
comment.
19. What are the rules for Variable names in
java
The rules for
declaring Variable names in java
Allowed
characters:
a. Alphabets
(A-Z, a-z)
b. Numbers
(0-9)
c. Underscore
(_)
d. Dollar
sign ($)
Not
allowed:
e. Blank
spaces
f.
Special characters other than underscore
and dollar sign
g. Keywords
(e.g., int, if, for)
First
character: Must be a letter or underscore, not a number.
20. How many datatypes available in Java. Explain
Java
has two main categories of data types:
1. Primitive
data types: There are 8 primitive data types:
·
byte: Stores 8-bit signed integers
(-128 to 127).
·
short: Stores 16-bit signed
integers (-32,768 to 32,767).
·
int: Stores 32-bit signed integers (-2,147,483,648
to 2,147,483,647).
·
long: Stores 64-bit signed integers
(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
·
float: Stores 32-bit
single-precision floating-point numbers.
·
double: Stores 64-bit
double-precision floating-point numbers.
·
char: Stores 16-bit Unicode
characters.
·
boolean: Stores true or false
values.
2.
Non-primitive data types: These are
reference types that represent objects. They are created using classes and can
store complex data structures like arrays, strings, and custom objects.
Examples include:
·
String: Represents a sequence of
characters.
·
Arrays: Represent collections of
elements of the same data type.
·
Classes: Represent user-defined
types that encapsulate data and behavior.
·
Interfaces: Define contracts for
methods and constants that can be implemented by classes.
22. “Java
Is Case sensitive language” Illustrate
1.
Java is a case-sensitive
programming language, which means that it distinguishes between upper case
and lower-case letters.
2.
This means that the case of
letters in your Java programs matters and even the slightest difference in
naming can indicate different objects.
3. For example, Java will treat three variables called
"endLoop", "Endloop", and "EndLoop" differently
23. Expand
IDE and JVM.
IDE: Integrated development Environment
JVM: Java
Virtual Machine
24. Mention
any two features of JAVA.
Platform Independent
Portable
25. Differentiate between Println and Print.
Println insert the new line character to the
output and but print do not in Java.
26. Write a program to print “Hello MOTOROLA” in JAVA.
public static
void main (String[] args) {
System.out.println(“Hello MOTOROLA”); }
Comments
Post a Comment