Академический Документы
Профессиональный Документы
Культура Документы
CONFIDENTIAL i
Infosys Limited Table of Contents
COPYRIGHT NOTICE
CONFIDENTIAL ii
Infosys Limited Table of Contents
Contents
MAILTO:E&R@INFOSYS.COM ............................................................................................ II
CONTEXT ................................................................................................................... 4
GUIDELINES ................................................................................................................ 4
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.
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
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
CONFIDENTIAL 5
Infosys Limited Table of Contents
break;
default: System.out.println("Something is wrong ");
}
}
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");
}
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);
}
CONFIDENTIAL 7
Infosys Limited Table of Contents
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.");
}
}
Practice Question 10
class Parent{
Parent(int num){
this.num = num;
}
CONFIDENTIAL 8
Infosys Limited Table of Contents
return num;
}
class CourseMgmt{
}
}
Practice Question 11
CONFIDENTIAL 9
Infosys Limited Table of Contents
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();
}
}
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