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
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
No comments:
Post a Comment