Below are the second set of core java practice questions helpful for OCJP/SCJP preparation.
11)
For
loop and operators(AND OR) preincrement
public
class
Test {
public
static
void
main(String[] args) {
int
a=0;
int
b=0;
for(short
index=0; index<5;index++){
if
((++a>2)|(++b>2)){
a++;
}
}
System.out.println(a+"
"+b);
}
}
Ans:-8
5
--------------------------------------------------------------------
12)
For loop and operators(AND,OR) preincrement
public
class
Test {
public
static
void
main(String[] args) {
int
a=0;
int
b=0;
for(short
index=0; index<5;index++){
if
((++a>2)&&(++b>2)){
a++;
}
}
System.out.println(a+"
"+b);
}
}
Ans:-6
3
-------------------------------------------------------------------------
13)For
loop and operators(AND,OR) postincrement
public
class
Test {
public
static
void
main(String[] args) {
int
a=0;
int
b=0;
for(short
index=0; index<5;index++){
if
((a++>2)||(b++>2)){
a++;
}
}
System.out.println(a+"
"+b);
}
}
Ans:-
7 3
-------------------------------------------------------------------------
14)For
loop and operators(AND,OR) postincrement
public
class
Test {
public
static
void
main(String[] args) {
int
a=0;
int
b=0;
for(short
index=0; index<5;index++){
if
((a++>2)||(b++>2)){
a++;
}
}
System.out.println(a+"
"+b);
}
}
Ans
7 5
---------------------------------------------------------------------------
15)For
loop and operators(AND,OR) postincrement
public
class
Test {
public
static
void
main(String[] args) {
int
a=0;
int
b=0;
for(short
index=0; index<5;index++){
if
((a++>2)&&(b++>2)){
a++;
}
}
System.out.println(a+"
"+b);
}
}
Ans
:- 5 2
----------------------------------------------------------------------------------
16)For
loop and operators(AND,OR) postincrement
public
class
Test {
public
static
void
main(String[] args) {
int
a = 10, b = 5;
for
(int
i = 0; i < 5; i++, b--) {
if
(a > 2) {
a--;
}
if
(a > 5) {
--a;
continue;
}
}
System.out.println(a
+ "
"
+ b);
}
}
Ans:-
3 0
-----------------------------------------------------------------------------------
17)switch
and local variables should be initialises
public
static
void
main(String[] args) {
String
strln1;
int
i = (int)(4*3);
switch(i){
case
0:
strln1
= "case
0";
break;
case
1:
strln1
= "case
1";
break;
case
2:
strln1
= "case
2";
break;
default:
System.out.println("in
default");
}
System.out.println(strln1);
Ans
:- in
default null
------------------------------------------------------------------------------------
18)switch
and local variables should be initialises
public
class
Test {
public
static
void
main(String[] args){
final
int
x=2;
for
(int
i = 0; i < 3; i++) {
switch(i)
{
case
x:System.out.println("Hello");
case
x-1:System.out.println("second");
break;
default:System.out.println("default");
case
x-2:System.out.println("fourth");
}
}
}
}
Ans:-
fourth
second
Hello
second
-----------------------------------------------------------------
19)
switch
and local variables should be initialises
public
class
Test {
public
static
void
main(String[] args){
int
x=2;
for
(int
i = 0; i < 3; i++) {
switch(i)
{
case
x:System.out.println("Hello");
case
x-1:System.out.println("second");
break;
default:System.out.println("default");
case
x-2:System.out.println("fourth");
}
}
}
}
Ans:-
compile
time error ,case statement needs a constant expression
----------------------------------------------------------------------------
20)
Local
variable and Instance variable
public
class
Test {
int
count;
public
void
method1(){
System.out.println("method
1");
}
public
static
void
main (String [] args){
int
count=12;
Test
obj= new
Test();
System.out.println(obj.count);
System.out.println(count);
}
}
Ans:-
0
12
Happy
Learning
Please
provide your valuable comments on this article and share it across
your network.
Contact
me @ sudheer@javarecent.com
or admin@java-recent.com
No comments:
Post a Comment