JAVA Demo
JAVA Demo
Arithmetic Exception :- Ex :
▪ It is Run Time Exception but not Compile Time System.out.println(10 < 20); // true
▪ It is possible only in integral data types but not System.out.println(‘a’ < 97.23); // true
▪ The only operators which cause Arithmetic System.out.println(true > false); // CTE:
67
Conclusion :- Ex. 3:- Predict Output:-
▪ (&, |, ^):- Applicable for both boolean and int x=10;
integral types. if(++x<10 & x/0>15){
▪ (~):- Applicable for only integral types but not System.out.println(“Hello”);
for Boolean types. }
▪ (!):- Applicable only for boolean type but not else{
for integral type. System.out.println(“Hi”);
}
8. Short – Circuit Operators (&&,||):-
Options:-
These are exactly same as bitwise operators (&, |)
1. Compile Time Error
except the following differences.
2. Run Time Exception (/ by zero)
&,| &&, ||
3. Hello
Both arguments are Second argument is
4. Hi
compulsory to optional. Depends on
check. operator. 9. Type Cast Operator :-
Relatively Relatively performance is There are two types of type casting:-
In java we have only new keyword but not delete operand precedence.
▪ Before apply any operator all operands will be
keyword because destruction of useless object is the
evaluated from left to right.
responsibility of garbage collector.
Ex : Find which method call will be evaluated first:-
13. [] Dimension Operators :- class Test
We can use this operator to declare and create {
arrays. public static void main(String[] args)
Ex : int[] x = new int[5]; {
14. Operator Precedence Table:- System.out.println(m1(1)+m1(2)*m1(3)/m1(
i. Unary Operators:- 4)+m1(5)*m1(6));
[], x++, x— }
++x, --x, ~, ! public static int m1(int n)
new, <type> {
ii. Arithmetic Operators:- System.out.print(n+” “);
*, /, % return 0;
+, - }
iii. Shift Operators:- }
>>, >>>, << Output:- 1 2 3 4 5 6 32
iv. Comparison Operators:- Solution:-
<, <=, >, >=, instanceof m1(1)+m1(2)*m1(3)/m1(4)+m1(5)*m1(6) will be
v. Equality Operators:- evaluated as below after operand evaluation.
==, != 1+2*3/4+5*6
70
1+6/4+5*6 argument). Object will be Created for
1+1+5*6 java.lang.String class.
1+1+30 Note:- forName(), getClass(), and getName() are
2+30 the members of class Class.
32 Class class:- Instances of the class Class represent
16. Difference between new V/s newInstance:- classes and interfaces in a running Java application.
▪ In the case of new operator, based on our
new Operator :-
requirement we can invoke any constructor.
we can use new operator to create an object if we
Ex : Test t = new Test();
know class name at the beginning.
Test t1 = new Test(10);
Ex : Test t = new Test();
Student s = new Student(); Test t2 = new Test(“Java”);
71
▪ The newInstance() method of Class class and To use new operator, To use newInstance()
Constructor class is used to create a new class is not required to method, it is compulsory
instance of the class. contain no-argument that class should have
▪ The newInstance() method of Class class can constructor. no-argument constructor
invoke zero-argument constructor only, whereas (Parameterized
newInstance() method of Constructor class can constructors are not
invoke any number of arguments. So, Constructor allowed), otherwise we
class is preferred over Class class. will get
72
18. Difference between instanceof V/s
isInstance :-
▪ Instanceof is an operator in java, we can use
instanceof to check whether the given object is
of a particular type or not. And we know the
type at the beginning.
Ex : Thread t = new Thread();
System.out.println(t instanceof Runnable);// true
System.out.println(t instanceof Object);// true
▪ isInstance() is method present in
java.lang.Class.
▪ we can use isInstance() method to check,
whether the given object is of particular type or
not, and we don’t know the type at the
beginning and it is available dynamically at
runtime.
Ex.:-
class Test{
public static void main(String[] args)
throws exception
{
Thread t = new Thread();
System.out.println(Class.forName(args[0]).i
sInstance(t));
}
}
Output:- Java Test Runnable (true)
Java Test String (false)
73
Important Questions
1. Which are the logical operators? 9. What is the output of the following code snippet?
(A) && (B) || int a = 5;
(C) All of the above (D) None of the above int b = 2;
2. Which of the following operators is used to perform int c = a / b;
decrement in Java? System.out.println(c);
(A) -- (B) + (A) 2 (B) 2.5
(C) * (D) & (C) 3 (D) Compilation error
3. What is the output of the following code snippet? 10. Which of the following operators is used to perform
int a = 5; logical OR in Java?
int b = 7; (A) & (B) &&
System.out.println((a > b) ? "a is greater than b" : "a (C) || (D) ^
is less than or equal to b"); 11. Which of the following operators is used to perform
(A) a is greater than b Bitwise OR in Java?
(B) a is less than or equal to b (A) | (B) ^
(C) Compilation error (C) & (D) ||
(D) Runtime error 12. Which of the following operators is used to perform
4. Which of the following operators is used to perform equality comparison in Java?
bitwise XOR in Java? (A) = (B) ==
(A) ^ (B) | (C) != (D) >
(C) & (D) ~ 13. Which of the following operators is used to perform
5. Which of the following is a unary logical operator in Bitwise AND in Java?
Java? (A) & (B) &&
(A) ! (B) & (C) | (D) ^
(C) | (D) ^ 14. Which of the following operators is used to perform
6. What is the output of the following code snippet? modulo division in Java?
int x = 10; (A) / (B) %
int y = 20; (C) * (D) &
int z = x++ + ++y; 15. Which of the following operators has the highest
System.out.println(z); precedence in Java?
(A) 31 (B) 2 (A) + (B) /
(C) 33 (D) 34 (C) ++ (D) &&
7. Which of the following is a conditional operator in 16. Which of the following is a unary operator in Java?
Java? (A) + (B) /
(A) = (B) * (C) - (D) *
(C) + (D) ? 17. Which of the following is a logical operator in Java?
8. Which of the following operators is used to perform (A) ++ (B) /
left shift in Java? (C) && (D) =
(A) << (B) >> 18. Which of the following is a relational operator in
(C) & (D) | Java?
74
(A) + (B) = (C) The destination type can be larger or smaller than
(C) == (D) && source type
19. What is the new operator? (D) None of the mentioned
(A) Allocates memory for an object or array 28. If a variable or operand in an expression is type long,
(B) Allocates memory for an object or array and then all the operands are type promoted to which data
returns a particular pointer type?
(C) Used as return type when an object is created (A) int (B) long
(D) Used to declare any new thing in a program (C) float (D) double
20. A Java Ternary operator has priority less than ____. 29. A boolean literal in java can be type casted to which
(A) Relational operator data type?
(B) Arithmetic operator (A) byte (B) short
(C) Logical and bitwise operator (C) int (D) None of the above
(D) All 30. Which are the compatible Data Types for Type
21. State TRUE or FALSE, TRUE expression part comes promotion or Type Casting?
first after ? (question mark) symbol and before : (A) byte, int, short (B) char, int, float
(colon) symbol. (C) float, long, double (D) All of the above
(A) FALSE (B) TRUE 31. What is the output of a bitwise OR (l) operation if
(C) Nothing can be said (D) None of these both the inputs are 1s?
22. The condition of the Java ternary operator should (A) 0 (B) 1
evaluate to ____ (C) 0 or 1 both (D) None of the above
(A) 1 or 0 (B) true or false 32. What is the output of a Bitwise AND (&) operation if
(C) TRUE or FALSE (D) None both the inputs / operands are 1s?
23. Java ternary operator is sometimes called ____. (A) 0 (B) 1
(A) Relational operator (B) Conditional operator (C) 0 or 1 both (D) None of the above
(C) Logical operator (D) None 33. What is the output of the Java code snippet?
24. What is the other name for a question mark –colon (? byte a= 0b0000_0001;
:) operator in java? System.out.println(~a);
(A) Special Relational operator (A) -1 (B) -2
(B) Special Logical operator (C) 254 (D) +127
(C) Ternary operator 34. Right Shift (>>) in Java is equivalent to ?
(D) none (A) Multipying the number by 2
25. Select from among the following character escape (B) Dividing the number by 2
code which is not available in Java. (C) Subtracting the number by 2
(A) \\ (B) \v (D) Adding the number by 2
(C) \a (D) \t 35. Left Shift (<<) in Java is equivalent to?
26. Evaluate the value of the expression? (A) Subtracting the number by 2
6 - 2 + 10 % 4 + 7 (B) Dividing the number by 2
(A) 14 (B) 12 (C) Multiplying the number by 2
(C) 13 (D) 10 (D) Adding the number by 2
27. Which of these is necessary condition for automatic 36. What is this >>> bitwise operator in Java?
type conversion in Java? (A) Left shift operator
(A) The destination type is smaller than source type (B) Left Shift Fill Zero operator
(B) The destination type is larger than source type (C) Right Shift operator
75
(D) Right Shift Fill Zero operator (B) Postfix operation is carried out on the next line or
37. Instanceof returns _____ if the value of the object is statement. So the variable value will not change.
null. (C) A and B
(A) -1 (B) 1 (D) None of the above.
(C) True (D) False 44. Among the operator groups (++, --) and (+,-,*,/,%) in
38. Which of the operators can be used to concatenate Java , which group has higher priority ?
two or more string objects? (A) (++, --) group has higher priority than (+,-,*,/,%)
(A) + (B) += group.
(C) & (D) ll (B) ( ++, --) group has lower priority than (+,-,*,/,%)
39. What is the right output of this program ? group.
public class ShiftOperators { (C) ( ++,--) group and (+,-,*,/,%) group have equal
public static void main(String[] args) { priority.
System.out.print(8<<2); (D) None of the above
System.out.print(","); 45. Choose the correct statement about Java operators +,-
System.out.print(8>>1); ,*,/,%.
} (A) + and – have equal priority.
} (B) * and / have equal priority.
(A) 4, 32 (B) 32,4 (C) / and % have equal priority
(C) 16,4 (D) 4,16 (D) All of the above.
40. What is the right output of this program? 46. Among Postfix decrement and Prefix increment
public class UnaryOperators { operators in Java, which operator has less priority?
public static void main(String[] args) { (A) Postfix decrement has less priority than prefix
int i = 5; increment
System.out.print(i++); (B) Prefix increment has less priority than Postfix
System.out.print(++i); decrement
System.out.print(i--); (C) Both operators have same priority
System.out.print(--i); (D) None of the above
} 47. Between Postfix and Prefix arithmetic operators in
} Java, which operators have more priority?
(A) 6765 (B) 5654 (A) Postfix operators have more priority than Prefix
(C) 5775 (D) 5765 operators.
41. Which are the relational operators? (B) Prefix operators have more priority than Postfix
(A) < < = (B) > > = instanceof operators.
(C) Both Prefix and Postfix operators have equal
(C) = = ! = (D) All of the above
priority.
42. Which are the unary operators?
(D) None of the above.
(A) ++ (B) --
48. Arithmetic operators +,-,/,* and % which
(C) Both of the above (D) None of these
associativity?
43. Choose the correct statement about Java Prefix and (A) Right to left (B) Left to right
Postfix operations. (C) Right t right (D) Left to left
(A) Prefix operation is carried out immediately and 49. What is the output of the javacode snippet?
the variable value will be incremented or Int a=5, b=10, c=15;
decremented accordingly. a -=3;
76
b *=2; int z;
c /=5; x=3;
system.out.println(a +” “ + b + “ “ + c); y=4;
(A) 2 20 3 (B) 2 20 5 z = ++x * y++;
(C) 2 10 5 (D) -2 20 3 (A) 2 (B) 3
50. What is the output of Java code snippet? (C) 4 (D) unknown/undefined
short k=1; 56. What value does the variable z have after ALL of the
k += 2; code above executes?
system.out.println(k); int x;
(A) 1 (B) 2 int y;
(C) 3 (D) Compiler error about Typecasting. int z;
51. What is the output of the below Java code snippet? x=3;
short p=1; y=4;
z = ++x * y++;
short k=p+2;
(A) 9 (B) 12
system.out.println(k);
(C) 16 (D) 20
(A) 1 (B) 2
57. What value does the variable a have after ALL of the
(C) 3 (D) Compile error
code above executes?
52. What is the output of the below Java code snippet?
int a;
Int a= 2 -- 7;
int b;
System.out.println(a); a=1;
(A) -5 (B) 10 b=a++;
(C) 9 (D) Compile error (A) 1 (B) 2
53. With x = 0, which of the following are legal lines of (C) 3 (D) unknown/undefined
Java code for changing the value of x to 1? 58. What value does the variable b have after ALL of the
x++; code executes?
x = x + 1; int a;
x += 1; int b;
x =+ 1;
a=1;
(A) 1, 2 & 3 (B) 1 & 4
b=a++;
(C) 1, 2, 3 & 4 (D) 3 & 2
(A) 1 (B) 2
54. What value does the variable y have after ALL of the
(C) 3 (D) unknown/undefined
code above executes?
int x; Answer key
int y; 1 2 3 4 5 6 7 8 9 10
int z; C A B A A C D A A C
11 12 13 14 15 16 17 18 19 20
x=3;
A B A B C C C C B D
y=4; 21 22 23 24 25 26 27 28 29 30
z = ++x * y++; B B B C C C B B D D
(A) 4 (B) 5 31 32 33 34 35 36 37 38 39 40
(C) 6 (D) unknown/undefined B B B B C D D A B C
55. What value does the variable x have after ALL of the 41 42 43 44 45 46 47 48 49 50
D C C A D B A B A C
code above executes?
51 52 53 54 55 56 57 58
int x; D C C B C C B A
int y;
77