Exam CoreJAVA
Exam CoreJAVA
6. What is the significance of the "Write Once, Run Anywhere" slogan in Java?
A. Java programs can be written in any language.
B. Java code can be executed on any operating system.
C. Java code can only be written once.
D. Java is a write-only language.
11.What is the purpose of the "public static void main(String[] args)" method in Java?
A. Entry point of a Java program
B. A utility method for printing text
C. A method for creating objects
D. A method for mathematical calculations
D. private
17.In the context of inheritance, which access modifier is often used with a final class?
A. public
B. private
C. protected
D. default
18 What is the purpose of using the "final" keyword with a variable in Java?
A. To indicate the variable's data type.
B. To prevent the variable from being modified after initialization.
C. To allow the variable to be accessed without restrictions.
D. To indicate the variable's value.
25 .Which operator is used to compare two values and return the maximum of the two
in Java?
A. >
B. >=
C. <
D. <=
33.Which operator is used to compare two values for equality without considering their
data types?
A. ==
B. ===
C. .equals()
D. !=
34.What is type casting in programming?
35.In Java, what is the purpose of explicit type casting (e.g., (int) someDouble)?
38.In Java, what happens if you try to cast an object of one class to another class if they
are not related by inheritance?
40 .In C#, which casting method is used to cast from a base class to a derived class?
A. Explicit casting
B. Implicit casting
C. is/as casting
D. Safe casting
41.Which of the following programming languages supports both implicit and explicit
type casting?
A. Python
B. Java
C. C++
D. JavaScript
A. To confuse programmers
B. To save memory
C. To improve code readability
D. To enable flexibility when working with different data types
Code :
import java.util.Scanner;
class CompareTwoNumbers {
public static void main(String[] args) {
int num1;
int num2;
Scanner s=new Scanner(System.in);
System.out.println("Enter number 1:");
num1=s.nextInt();
System.out.println("Enter number 2:");
num2=s.nextInt();
if(num1==num2)
{
System.out.println(num1 +" and "+ num2 +"are equal");
}
if(num1<num2)
{
System.out.println(num1 +" is lessthan "+ num2);
}
if(num1>num2)
{
System.out.println(num1+" is Greaterthan "+num2);
}
}
}
Output :
44.Write a Java program that takes two boolean values as input. Implement logical AND,
OR, and NOT operations on these values and display the results.
Code:
import java.util.Scanner;
class LogicalOperators {
public static void main(String[] args) {
Boolean x;
Boolean y;
Scanner s=new Scanner(System.in);
System.out.println("Enter x value (true/false):");
x=s.nextBoolean();
System.out.println("Enter y value (true/false):");
y=s.nextBoolean();
Boolean result_AND=x&&y;
System.out.println("Logical AND result is:\n"+result_AND);
Boolean result_OR=x||y;
System.out.println("Logical OR result is:\n"+result_OR);
Boolean result_NOT=!x;
System.out.println("Logical NOT result is:\n"+result_NOT);
}
}
Output:
45 .Write a Java program that demonstrates the use of the increment (++) and
decrement (--) operators. Use these operators to increase and decrease a variable's
value and display the results.
Code:
import java.util.Scanner;
class IncrementAndDecrement {
public static void main(String[] args) {
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x value:");
x=s.nextInt();
int PostInc=x++;
System.out.println("X value After Post Increment: "+PostInc);
int PreInc=++x;
System.out.println("X value After Pre Increment: "+PreInc);
int PostDec=x--;
System.out.println("X value After Post Decrement: "+PostDec);
int PreDec=--x;
System.out.println("X value After Pre Decrement: "+PreDec);
}
}
Output:
46.Write a Java program that takes two numbers as input and uses the conditional
operator to find and display the maximum of the two numbers.
Code:
import java.util.Scanner;
class MaximumOfTwoNumbers {
public static void main(String[] args) {
int num1;
int num2;
Scanner s=new Scanner(System.in);
System.out.println("Enter number 1:");
num1=s.nextInt();
System.out.println("Enter number 2:");
num2=s.nextInt();
int max=(num1>num2)?num1:num2;
System.out.println("The Maximum number is:\n"+max);
}
}
Output:
47.Write a Java program that demonstrates the use of bitwise AND, OR, XOR, left shift,
and right shift operators on integer values. Display the results of each operation.
Code:
import java.util.Scanner;
class BitwiseOperators {
public static void main(String[] args) {
int x;
int y;
Scanner s=new Scanner(System.in);
System.out.println("Enter x value:");
x=s.nextInt();
System.out.println("Enter y value:");
y=s.nextInt();
int result_AND=x&y;
System.out.println("Bitwise AND result is:\n"+result_AND);
int result_OR=x|y;
System.out.println("Bitwise OR result is:\n"+result_OR);
int result_XOR=x^y;
System.out.println("Bitwise XOR result is:\n"+result_XOR);
int Left_Shift=x<<2;
System.out.println("Bitwise Left Shift result is:\n"+Left_Shift);
int Right_Shift=x>>2;
System.out.println("Bitwise Right Shift result is:\n"+Right_Shift);
}
}
Output:
48.Write a Java program that uses various assignment operators, such as +=, -=, *=, /=,
and %=, to modify the values of variables and display the results.
Code:
import java.util.Scanner;
class AssignmentOperator {
public static void main(String[] args) {
int x;
Scanner s=new Scanner(System.in);
System.out.println("Enter x value:");
x=s.nextInt();
x+=10;
System.out.println("after using +=,the x value is: "+x);
x-=20;
System.out.println("after using -=,the x value is: "+x);
x*=19;
System.out.println("after using *=,the x value is: "+x);
x/=10;
System.out.println("after using /=,the x value is: "+x);
x%=3;
System.out.println("after using %=,the x value is: "+x);
}
}
Output:
49.Write a Java program that demonstrates the order of precedence of operators in a
complex expression. Create an expression that involves arithmetic, relational, and
logical operators, and display the result.
Code:
Output:
Code: