Monday, September 16, 2013

Core Java practice questions-2

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.


Tuesday, September 3, 2013

SCJP/OCJP model questions-1

1)class operators{
public static void main(String args[]){
int a=10;
int b=15;
System.out.println("a+b"+a+b); //1

System.out.println(a+b); //3
}
}

what will be the out put of the program _________and__________

2)class test
{
class student{
int i=1;
int j=2;
{
System.out.println("in second class");
}
public static void main(String args[]){
  }
}
}

//sataic methods or variables cannot be declared in ineer classes
-----------------------------------------------------------------------------------------------------------


3)  class Testing
{

private int x=5;
private int y=10;

Testing(int x,int y)
{
System.out.println("inside constructor");
}

public void add()
{
System.out.println("sum"+" "+(x+y));
}

}


 public class Caller {

Testing testing =new Testing();
testing.add();

}

Ans:-compile time error
------------------------------------------------------------------
4)public class Caller{
public static void main(String args[]) {
Test test = new Test(5,5);
test.disp();
}
}
class Test{
public int a;
public int b;
Test(int a, int b) {
a=a;
b=b;
}
public void disp(){
System.out.println(a+" "+b);
}
}

a)5,5 b) compile time error c)run time exception 4) 0 0
----------------------------------------------------------------------

This operator

5)public class Caller{
public static void main(String args[]) {
Test test = new Test(5,5);
test.disp();
}
}
class Test{
public int a;
public int b;
Test(int a, int b) {
this.a=a;
b=b;
}
public void disp(){
System.out.println(a+" "+b);
}
}
Ans:- 5 0
-----------------------------------------------------------------
6)Inheritance
class Parent{
int i = 2;
int j = 3;
}

class Child extends Parent{
int i = 4;
int j = 5;
}

public class Caller{
public static void main(String args[]){
Child child;
Parent parent;
child = new Child();
parent = new Parent();
parent = child;
System.out.println(child.i);
System.out.println(parent.i);
System.out.println(child.j);
System.out.println(parent.j);
}
}
Ans :-4 2 5 3

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Exception
7)class Parent{
int i = 2;
int j = 3;
}

class Child extends Parent{
int i = 4;
int j = 5;
}

public class Caller{
public static void main(String args[]){
Child child;
Parent parent;
child = new Child();
parent = new Parent();

child=(Child) parent;
System.out.println(child.i);
System.out.println(parent.i);
System.out.println(child.j);
System.out.println(parent.j);
}
}

Ans :- Run time exception(Class cast exception)
-------------------------------------------------------------------------------------------------------------------------------------------------------

8) constructor
public class Test {

public static void main(String[] args) {
Call call = new Call(10);
call.disp();
}
}

class Call {
public int a;
public char b;

Call(char b) {
System.out.println("inside default constructor");
this.b = b;
System.out.println(b);
}

Call(int a) {

this('s');
this.a = a;
}

public void disp() {
System.out.println(a + " " + b);
}
}

Guess the o/p______________________
Ans:- inside default constructor
10 s
---------------------------------------------------------------

9)public class Test {

public static void main(String[] args) {
int i=1;
if (i)
System.out.println("true");
else
System.out.println("false");
}
}

Ans:- compile time error .
-----------------------------------------------------------------
10) 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  2

Like and Share