Вы находитесь на странице: 1из 13

Infosys Limited Table of Contents

Practice Guide for OO Concepts using Java

CONFIDENTIAL i
Infosys Limited Table of Contents

COPYRIGHT NOTICE

© 2009-2011 Infosys Limited, Bangalore, India. All rights reserved.


Infosys believes the information in this document is accurate as of its publication
date; such information is subject to change without notice. Infosys acknowledges the
proprietary rights of other companies to the trademarks, product names and such
other intellectual property rights mentioned in this document. Except as expressly
permitted, neither this document nor any part of it may be reproduced, stored in a
retrieval system, or transmitted in any form or by any means, electronic,
mechanical, printing, photocopying, recording or otherwise, without the prior
permission of Infosys Limited and/or any named intellectual property rights holders
under this document.

Education and Research Department


Infosys Limited
Electronics City
Hosur Road
Bangalore - 561 229, India.

Tel: 91 80 852 0261-270


Fax: 91 80 852 0362
www.infosys.com
mailto:E&R@infosys.com

CONFIDENTIAL ii
Infosys Limited Table of Contents

Contents

MAILTO:E&R@INFOSYS.COM ............................................................................................ II

CONTENTS ................................................................................................................. III

CONTEXT ................................................................................................................... 4

GUIDELINES ................................................................................................................ 4

PRACTICE QUESTIONS - DAY 2 ......................................................................................... 4


Practice Question 1 ................................................................................... 4
Practice Question 2 ................................................................................... 4
Practice Question 3 ................................................................................... 5
Practice Question 4 ................................................................................... 5
PRACTICE QUESTIONS - DAY 3 ......................................................................................... 6
Practice Question 5 ................................................................................... 6
Practice Question 6 ................................................................................... 6
Practice Question 7 ................................................................................... 7
Practice Question 8 ................................................................................... 7
PRACTICE QUESTIONS - DAY 4 ......................................................................................... 8
Practice Question 9 ................................................................................... 8
Practice Question 10 ................................................................................. 8
Practice Question 11 ................................................................................. 9
PRACTICE QUESTIONS - DAY 5 ....................................................................................... 10
Practice Question 12 ................................................................................ 10
Practice Question 13 ................................................................................ 11
Practice Question 14 ................................................................................ 11
PRACTICE QUESTIONS - DAY 6 ....................................................................................... 12
Practice Question 15 ................................................................................ 12

CONFIDENTIAL iii
Infosys Limited Table of Contents

Context
This document contains Practice Questions to be completed as part of the practice session for
the module Object Oriented Concepts (OOC) using Java.

Guidelines

• The practice guide has been designed to give hands on experience to map the
concepts learnt in the theory session with practical Practice Questions and provide
more hands on practice
• The Practice Questions are organized based on topics covered day-wise.
• These Practice Questions contain coding exercises, debugging exercises, and many
code snippets for analyzing and predicting the output
• Solving these exercises methodically would provide more practice and hence
confidence to the learner to attempt the exams.
• Practice Questions must be submitted at the end of the OOC using Java module.

Practice Questions - Day 2

Predict the output of the following programs and analyze your answer by executing the code.
If there is an error, find the reason for the error and debug the same.

Practice Question 1

class Example{
public static void main(String[] args){
int intValOne = 0;
int intValTwo = 0;
for(short index=0; index < 5; index++){
if((++intValOne > 2) || (++intValTwo > 2)){
intValOne++;
}
}
System.out.println(intValOne + " " + intValTwo);
}
}

Practice Question 2

class Example{

CONFIDENTIAL 4
Infosys Limited Table of Contents

public static void main(String[] args){


int intValOne = 5,intValTwo = 7;
System.out.println((( intValTwo * 2) % intValOne));
System.out.println(intValTwo % intValOne);
}
}

Practice Question 3

class Example{
public static void main(String[] args){
int val1 = 9;
int val2 = 6;
for (int val3 = 0; val3 < 6; val3++,val2--){
if(val1 > 2){
val1--;
}
if(val1 > 5){
System.out.print(val1 + “ “);
--val1;
continue;
}
val1--;
}
}
}

Practice Question 4

public class SwitchCase {


public static void main(String args[]){
int val = (int)(4 * 3);
switch (val) {
case 0: System.out.println("Case 0");
break;
case 1: System.out.println("Case 1");
break;
case 2: System.out.println("Case 2");

CONFIDENTIAL 5
Infosys Limited Table of Contents

break;
default: System.out.println("Something is wrong ");
}
}

Practice Questions - Day 3

Predict the output of the following programs and analyze your answer by executing the code.
If there is an error, find the reason for the error and debug the same.

Practice Question 5

class StringEqual{
public static void main(String arg[]){

String name1,name2;
name1 = new String("God");
name2= new String("God");
if(name1.equals(name2)){
System.out.println("Equal");
}
else{
System.out.println("Not Equal");
}
}
}

Practice Question 6

class StaticDemo
{
static int count=10;

StaticDemo(){
count++;
}
static void display(){
System.out.println(count);
}

CONFIDENTIAL 6
Infosys Limited Table of Contents

static{
System.out.println("Static Block written Before Main");
}

public static void main(String args[]){


StaticDemo t1=new StaticDemo();
StaticDemo t2=new StaticDemo();

StaticDemo.display();
t2.display();

}
static{
System.out.println("Static Block written After Main");
}
}

Practice Question 7

class MultiDimArray {
public static void main(String[] args) {
String[][] names = {{"Mrs.","Ms." , "Mr."},
{"Leo","James","John","Lena"}};
System.out.println(names[0][0] + names[1][0]);
System.out.println(names[0][2] + names[1][2]);
}
}

Practice Question 8

class StaticDemo1{
private int num;
static void display(){
System.out.println(num);
}

public static void main(String args[]){


StaticDemo1.display();
}
}

CONFIDENTIAL 7
Infosys Limited Table of Contents

Practice Questions - Day 4

Predict the output of the following programs and analyze your answer by executing the code.
If there is an error, find the reason for the error and debug the same.

Practice Question 9

class Animal {
public static void testClassMethod() {
System.out.println("The class method in Animal.");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal.");
}
}

class Cat extends Animal {


public static void testClassMethod() {
System.out.println("The class method in Cat.");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat.");
}

public static void main(String[] args) {


Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}

Practice Question 10

class Parent{

private int num;

Parent(int num){
this.num = num;
}

public int getNum(){

CONFIDENTIAL 8
Infosys Limited Table of Contents

return num;
}

class Child extends Parent{

private int val;

Child(int num,int val){


// To invoke super class constructor
super(num);
this.val = val;
}
public int getVal(){
return val;
}
}

class CourseMgmt{

public static void main(String args[]){


Child child;

child = new Child(100,200);


System.out.println("Parent: Num:" + child.getNum());
System.out.println("Child: Val:" + child.getVal());

}
}

Practice Question 11

public class ClassA {


public void methodOne(int i) {
}
public void methodTwo(int i) {
}
public static void methodThree(int i) {
}
public static void methodFour(int i) {
}

CONFIDENTIAL 9
Infosys Limited Table of Contents

public class ClassB extends ClassA {


public static void methodOne(int i) {
}
public void methodTwo(int i) {
}
public void methodThree(int i) {
}
public static void methodFour(int i) {
}
}

Practice Questions - Day 5

Predict the output of the following programs and analyze your answer by executing the code.
If there is an error, find the reason for the error and debug the same.

Practice Question 12

interface ParentOne{
int pOne=1;
void printParentOne();
}
interface ParentTwo{
int pTwo=2;
void printParentTwo();
}
interface Child extends ParentOne, ParentTwo{
int child=3;
void printChild();
}
class InheritClass implements Child{
public void printParentOne(){
System.out.println(pOne);
}
public void printParentTwo(){
System.out.println(pTwo);
}
public void printChild(){
System.out.println(child);
}

CONFIDENTIAL 10
Infosys Limited Table of Contents

}
class TestInterface{
public static void main(String args[]){
InheritClass inclass=new InheritClass();
inclass.printParentOne();
inclass.printParentTwo();
inclass.printChild();
}
}

Practice Question 13

class Example{
public final int VAR;
public void disp(){
System.out.println("disp in Example");
}
}
class Example1 extends Example{
public void disp(){
System.out.println("disp in Example1");
System.out.println(VAR);
}
}
class Demo{
public static void main(String args[]){
Example1 obj=new Example1();
obj.disp();
}
}

Practice Question 14

class Example{
public final int VAR;
public void disp(){
System.out.println("disp in Example");
}
}
class Example1 extends Example{
public final void disp(){
System.out.println("disp in Example1");

CONFIDENTIAL 11
Infosys Limited Table of Contents

System.out.println(VAR);
}
}
class Demo{
public static void main(String args[]){
Example1 obj=new Example1();
obj.disp();
}
}

Practice Questions - Day 6

Predict the output of the following programs and analyze your answer by executing the code.
If there is an error, find the reason for the error and debug the same.

Practice Question 15

package pack1;
public class Base{
private int privateNum;
int defaultNum;
protected int protectedNum;
public int publicNum;
public Base(){
privateNum=90;
defaultNum=900;
protectedNum=9000;
publicNum=90000;
}
}

package pack3;
import pack1.Base;
class Sample{
public void disp(){
Base obj1=new Base();
System.out.println(obj1.privateNum);
System.out.println(obj1.defaultNum);
System.out.println(obj1.protectedNum);

CONFIDENTIAL 12
Infosys Limited Table of Contents

System.out.println(obj1.publicNum);
}
}
class Demo1{
public static void main(String args[]){
Sample obj=new Sample();
obj.disp();
}
}

CONFIDENTIAL 13

Вам также может понравиться