String Functions Made Easy

 String Functions Made Easy

/**

* Project Name : String Functions Made Easy

* Author : Mr. Sumit Kole.

*/

import java.util.*;

class StringFuntionMadeEasy

{

String result,userInput1, userInput2;

char resultChar;

Scanner sc;

int choice;

public StringFuntionMadeEasy()

{

// initialise instance variables

result="";

userInput1=" ";

userInput2=" ";

resultChar='\u0000';

choice=0;

sc=new Scanner(System.in);

}

public void menu(){

System.out.println("\n\t\t\t ------String Functions Made Easy----

-");

System.out.println("\n\t\t\t Press 1 to Demonstrate CharAt()");

System.out.println("\n\t\t\t Press 2 to Demonstrate

compareTo()");

System.out.println("\n\t\t\t Press 3 to Demonstrate startsWith()

");

System.out.println("\n\t\t\t Press 4 to Demonstrate endsWith()

");

System.out.println("\n\t\t\t Press 5 to Demonstrate equalto() ");

System.out.println("\n\t\t\t Press 6 to Demonstrate

equalsIgnoreCase()");

System.out.println("\n\t\t\t Press 7 to Demonstrate indexOf() ");

System.out.println("\n\t\t\t Press 8 to Demonstrate lastIndexOf()

");

System.out.println("\n\t\t\t Press 9 to Demonstrate replace()");

System.out.println("\n\t\t\t Press 10 to Demonstrate

toUpperCase()");

System.out.println("\n\t\t\t Press 11 for of Exit");

System.out.println("\n\t\t\t

_____________________________________");

System.out.print("\n\t\t\t Please Enter Your Choice: ");

try{

sc=new Scanner(System.in);

choice = sc.nextInt();

sc.close();

if(choice<1 || choice>11){

System.out.println("\t\t\t Please Enter Numbers between 1

to 11 Only..");

menu();

}

operateMenu(choice);

}

catch(InputMismatchException e){

System.out.println("\t\t\t Please Enter Numbers Only..");

sc.close();

menu();

}

}

public String get(){

sc = new Scanner(System.in);

String input=sc.nextLine();

sc.close();

return input;

}

public void operateMenu(int choice){

switch(choice) {

case 1:

demo_charAt();

break;

case 2:

demo_compareTo();

break;

case 3:

demo_startsWith();

break;

case 4:

demo_endsWith();

break;

case 5:

demo_equals();

break;

case 6:

demo_equalsIgnoreCase();

break;

case 7:

demo_indexOf();

break;

case 8:

demo_lastIndexOf();

break;

case 9:

demo_replace();

break;

case 10:

demo_toUpperCase();

break;

case 11: System.exit(0);

default: System.out.println("Please enter number between 1 to

10");

}

System.out.println("\n \t\t\t Press 1 to Continue 0 to Exit ");

int number = Integer.parseInt(get());

if(number == 1){

menu();

}

else{

System.exit(0);

}

}

private void demo_charAt(){

System.out.println("\n\t\t\t Returns the character at the

specified index.");

System.out.print("\n\t\t\t Please Enter String ");

userInput1 = get();

System.out.print("\n\t\t\t Please Enter Index Number of character

which you want ");

int index = Integer.parseInt(get());

resultChar=userInput1.charAt(index);

System.out.println("\n\t\t\t Character At "+index+" =

"+resultChar);

}

private void demo_compareTo(){

System.out.println("\n\t\t\t Compares two strings

lexicographically.");

System.out.print("\n\t\t\t Please Enter First String ");

userInput1 = get();

System.out.print("\n\t\t\t Please Enter Second String ");

userInput2 = get();

if(userInput1.compareTo(userInput2)==0){

System.out.println("\n\t\t\t Entered Strings are Equal");

}

else{

System.out.println("\n\t\t\t Entered Strings are Not Equal");

}

}

private void demo_startsWith(){

System.out.println("\n\t\t\t Tests if this string starts with the

specified prefix.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Prefix :");

userInput2=get();

if(userInput1.startsWith(userInput2)){

System.out.print("\n\t\t\t Entered String Starts with "+

userInput2);

}

else{

System.out.print("\n\t\t\t Entered String Does Not Starts

with "+ userInput2);

}

}

private void demo_endsWith(){

System.out.println("\n\t\t\t Tests if this string ends with the

specified suffix.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Suffix :");

userInput2=get();

if(userInput1.endsWith(userInput2)){

System.out.print("\n\t\t\t Entered String Ends With "+

userInput2);

}

else{

System.out.print("\n\t\t\t Entered String Does Not Ends With

"+ userInput2);

}

}

private void demo_equals(){

System.out.println("\n\t\t\t Compares two strings are equal or

not.");

System.out.print("\n\t\t\t Enter First String For Comparison

:");

userInput1=get();

System.out.print("\n\t\t\t Enter Second String For Comparison

:");

userInput2=get();

if(userInput1.equals(userInput2)){

System.out.print("\n\t\t\t Entered Strings are Equal");

}

else{

System.out.print("\n\t\t\t Entered String are not Equal");

}

}

private void demo_equalsIgnoreCase(){

System.out.println("\n\t\t\t Compares this String to another

String, ignoring case considerations.");

System.out.println("\n\t\t\t Compares two strings are equal or

not.");

System.out.print("\n\t\t\t Enter First String For Comparison

:");

userInput1=get();

System.out.print("\n\t\t\t Enter Second String For Comparison

:");

userInput2=get();

if(userInput1.equalsIgnoreCase(userInput2)){

System.out.print("\n\t\t\t Entered Strings are Equal");

}

else{

System.out.print("\n\t\t\t Entered String are not Equal");

}

}

private void demo_indexOf(){

System.out.println("\n\t\t\t Returns the index of the first

occurrence of the specified character.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Character :");

char charInput= get().charAt(0);

if(charInput !='\u0000'){

System.out.print("\n\t\t\t First Occurence Of The Specified

Character ="+userInput1.indexOf(charInput));

}

else{

System.out.print("\n\t\t\t Enter valid character.");

}

}

private void demo_lastIndexOf(){

System.out.println("\n\t\t\t Returns the index within this string

of the last occurrence of the specified character.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Character :");

char charInput= get().charAt(0);

if(charInput !='\u0000'){

System.out.print("\n\t\t\t Last Occurence Of The Specified

Character ="+userInput1.lastIndexOf(charInput));

}

else{

System.out.print("\n\t\t\t Enter valid character.");

}

}

private void demo_replace(){

System.out.println("\n\t\t\t Returns a new string resulting from

replacing all occurrences of old Char in this string with new Char.");

System.out.print("\n\t\t\t Enter String :");

userInput1=get();

System.out.print("\n\t\t\t Enter Old Character :");

char oldChar= get().charAt(0);

System.out.print("\n\t\t\t Enter New Character :");

char newChar= get().charAt(0);

if((oldChar !='\u0000'&& newChar !='\u0000')){

System.out.print("\n\t\t\t New String After Replacing All

Occurrences of Old Char With New Char

:"+userInput1.replace(oldChar,newChar));

}

else{

System.out.print("\n\t\t\t Enter valid character.");

}

}

private void demo_toUpperCase(){

System.out.println("\n\t\t\t Converts all of the characters in

this String to upper case ");

System.out.print("\n\t\t\t Enter String to Convert in to

Uppercase :");

userInput1=get();

System.out.print("\n\t\t\t Converted String

:"+userInput1.toUpperCase());

}

public static void main(String args[])

{

StringFuntionMadeEasy ob = new StringFuntionMadeEasy();

ob.menu();

}

}

SAMPLE QUESTION PAPER 1

Sample Question Paper 1 

COMPUTER APPLICATIONS

Maximum Marks: 50

Time allowed: One hour (inclusive of reading time)

ALL QUESTIONS ARE COMPULSORY.

The marks intended for questions are given in brackets [ ].

Select the correct option for each of the following questions.

SECTION A (30 Marks)

Question 1) Choose the correct answer                                                                                     [5×1]

1)The default return type of a User defined function is

(a) float

(c) Void

(b) int

(d) Null

2) A constructor

(a) Must have the same name as the class it is declared within.

(b) Is used to create objects.

(c) May be declared private

(d) All of above

3) Default value of int data type is

(a) 0

(c) 0.0

(b) 1

(d) Null

4) How many times 'Hello' is  printed ?

    public static void main(String args[]) {

    for(i=0;i<5;i++){

        System.out.println("hello");

        break;

        }

    }

          (a) 5

(c) 4

(b) 1

(d) 0

5) The keyword used to return a value from a function is:

(a) returns

(c) send

(b) return

(d) None 

Question 2) Fill in the blanks with the correct option                                                               [5×1]

1) Multiple branches is used by __________ statement

(a) Break

(b) Switch

(c) Continue

(d) Loop

2) Java compiler javac translates Java source code into _________

(a) Assembler language

(b) Byte code

(c) Bit code

(d) Machine code

3) What is the result of 12 % 10 = _________

(a) 1

(b) 2

(c) 12

(d) 0

4) Byte Data Type range is _________

(a) -127 to 128

(b) -128 to 128

(c) -127 to 127

(d) -128 to 127

5)  A _________ class is accessible both inside and outside a package.

(a) public

(b) protected

(c) Super class

(d) open

Question 3) Name the following                                                                                                     [5×1]
1) The keyword that distinguishes between instance variable and class variable is:

(a) public

(b) protected

(c) static
2) A package which includes Math class

(a) Java.io

(b) Java. lang

(c) Java .math

3) A Scanner class method which is used to input real number

(a) nextFloat( )

(b) nextChar( )

(c) nextReal( )

4) A primitive data type which is used to store true or false value.

(a) String

(b) boolean

(c) int

5) A operator type which is used for comparison

(a) Arithmetic Operator

(b) Increment Operator

(c) Relational Operator

Question 4) State True Or False                                                                                                  [5×1]

1) Math.ceil() is an user defined function

(a) True

(b) False

2) User defined functions reside in classes

(a) True

(b) False

3) The default return type for a User defined function is Boolean 

(a) True

(b) False 

4) Encapsulation adds the function in a user-defined structure.

(a) True

(b) False

5) next( ) method is used to input more than two strings from the user.

(a) True

(b) False

Question 5) Choose the odd one                                                                                                  [5×1]

1)

(a) Use of pointers

(b) Dynamic 

(c) Object-oriented

(d) Secured

2)

(a) System

(b) Object

(c) Main

(d) strictíp 

3)

(a) int var;

(b) int VAR: 

(c) int 1 var;

(d) int varl;

4)
(a) next( )

          (b) nextInt( )

(c) pow( )

(d) nextFloat( )

5)  

(a) <=
 
(b) +

          (c) /

          (d) ++

Question 6) Give the output of the following                                                                             [5×1]
1)  
public class TestConstructor 
{
    void TestConstructor()
    {
        System.out.println("London");
    }
    TestConstructor()
    {
        System.out.println("America");
    }
    public static void main(String[] args)
    {
        TestConstructor tc = new TestConstructor();
    }
}

(a) America

(b) London

(c) No output

2) 
void sum(int num)
{
    for(i=1; i<=num; i++)
     {
        s=s+num*i
     }
}
    void main()
{
        sum(5);
}

(a) 75

(b) No output

(c) 80

3) Evaluate the following expression, if x =3,  y = 5, and z = 10;
    ++z + y - y + z + x++

(a) 23

(b) 24

(c) 25

4) If int  x = 15, y = 20 then what will be the value of x

    x = ( x < y) ? (y + x) : ( y - x) ;

(a) 5

(b) 25

(c) 35 

5) Math.floor (46.6) output is ?

(a) 46.6

(b) 46.0

(c) 47.0 

                                                                  SECTION B  (20 Marks) 

Question 7) Fill in the blanks of the given program with appropriate java statements         [6×1]

Given below is a class with the following specifications:

Class name : SumNum

Member Methods:

void sum ( ) – to print the sum of the following series :

sum ( ) = 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10

void sum(int m , int n) – to calculate sum of number between [m,n]

sum( 4,6) = 4 + 5 + 6

Fill in the blanks of the given program with appropriate java statements –

class SumNum

{

void sum ( )

{

int k;

int sum = 0;

for ( k = 1 ; (a)_________; k++)

{

if((b)___________)

(c)__________;

else

(d)__________;

}

System.out.println(sum);

}

void sum( int m, int n)

{

int sum = 0;

for((e) ________ : (f)_______; i++)

sum = sum + i;

System.out.println(sum);

}

}

(a)

     1. k < 10                                                                                                                         

     2. k < = 10

     3. k > 10

(b) 

      1. k % 3 == 0                                                                                                               

      2. k % 2 == 0;

      3. k % 2 == 1;

(c) 

      1. sum = sum + k ;                                                                                                    

      2. sum = sum - k ;

      3. sum = sum + 1 ;

(d) 

      1. sum = sum + k ;                                                                                                                      

      2. sum = sum - k ;

      3. sum = sum + 1 ;

(e) 

     1. int i = n                                                                                                                                  

     2. int i = m

     3. int i = 1

(f) 

     1. i < = m                                                                                      

     2. i < = n

     3. i < = 10 

Question 8) Fill in the blanks of the given program with appropriate java statements         [6×1]

The following program is based on the specification given below. 

Fill in the blanks with appropriate java statements.

class name : Student

member variables : String studentName [Name of the Student]

int marks [ Marks obtained ]

char grade [ grade awarded ]

Member methods :

void enterMarks( ) – to enter name of student and assign marks to the student through keyboard

void print() – to print the student name and marks assigned

void awardGrade() – grade is awarded to the student based on his/her marks.

             Marks range                             Grade

○ Greater than 91                             A

○ Greater than or equal to 80           B

○ Greater than 70                             C

○ Greater than 50                             D

○ Less than 50                                  F

void main ( ) – to create an object of the class and invoke the methods of the class

class Student

String studentName; int marks ; char grade;

Scanner s= new Scanner(System.in);

void enterMarks( )

{

    System.out.println(“Enter Number of the Student”);

    studentName = (a)___________;

    System.out.println(“Enter the marks obtained“);

    marks = (b) ____________ ;

}

void awardGrade()

{

    if ( marks > 91)

    grade = ‘A’;

    (c)______________

    grade = ‘B’;

    else if(marks > 70)

    grade = ‘C’;

    else if(marks > 50)

    grade = ‘D’;

    (d) ________

    grade = ‘F’;

}

void print()

{

    System.out.println(“Student Name = ”+studentName);

    (e)_______________________ ;

}

void main ()

{

    Student s = new Student();

    (f)____________;

    s.awardGrade();

    s.print();

}

}

(a) 

1. s.next() 

2. s.nextLine()

3. s.next().charAt(0) 

(b) 

1. s.next() 

2. s.nextLine()

3. s.nextInt()

(c) 

1. else if(marks >80)

2. else if ( marks > = 80 )

3. else if( marks > 70 ) 

(d)

1. else if ( marks > 50) 

2. else if ( marks > 50)

3. else

(e) 

1. System.out.println(“Marks obtained :”+marks) ;

2. System.out.println(“Student is excellent”);

3. System.out.println(“Student is bad”) ;

(f) 

1. s.enterMarks() 

2. s.enterInput()

3. s.awardGrade()

Question 9) Fill in the blanks of the given program with appropriate java statements         [4×1]

The following program is based on the specification given below. 

Fill in the blanks with appropriate java statements.

class name: hello

we have first initialized the two variables i and k with the values 2 and 1 respectively, while loop is used with the condition ++ i < 6. Thus, the value of i will increase by 1 before it can be used further because of a preceding ++ operator in the condition then assignment operator will be used between k and i. So the value of k will become 3.this will continue till the value of k will become 60.

class ____(a)____{

public static ____(b)____main (String args[])

{

    int i = ____(c)____, k=1;

    while (++i < 6)

        ____(d)____

    System.out.printin(k);

}

}

(a)

(i) hello

(ii) class

(iii) void

(b) 

(i) class 

(ii) void

(iii) object

(c)

 (i) i = 1

(ii) i = 2 

(ii) i = 0

(d)

(i) k *  = i;

(ii) i * = k;

(iii) k = i;

Question 10) Case Study                                                                                                             [4×1]

Java offers a rich set of operators to manipulate variables. Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc. Operators are special symbols that execute specific operations on one, two, or three operands and then return a result. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence seem in a similar expression, a rule must govern, which is assessed first. Entirely binary operators except for the assignment operators are assessed from left to right; assignment operators are evaluated right to the left. *= is an assignment operator. It uses ternary an arithmetic operator like +, -, *, or /, and the assignment operator (a single '=').

(a) 

int k=1,i=2

while (++i<6)

    k*=i;

System.out.println(k);

Predict the output

(i) 30

(ii) 60

(iii) 20

(iv) 50

(b) k*-i; is equal to

(i) k = k * i;

(iii) k = k - i;

(ii) k = k + i;

(iv) k = k/i;

(c) ++ is a ______

(i) Increment operator

(ii) Decrement operator 

(iii) Arithmetic operator 

(iv) None of the above

(d) Types of Integers are.

(i) short

(ii) int

(iii) long

(iv) all of the above

Chapter 3 Values and Data Types

 Chapter 3 

Values and Data Types

Question 1 ) Choose the correct option from the following. 

1) A _________ is the smallest element of a program that is meaningful to the compiler.

(a) Literal

(b) Token

(c) Variable

(d) Data

2) Literal are also known as ___________

(a) Tokens

(b) Identifiers

(c) Constants

(d) None of the above

3) There are _____ primitive data types in Java.

(a) 4

(b) 6

(c) 8

(d) 10

4) Boolean literals consist of the keywords; ______and _____

(a) true, false

(b) new, this

(c) do, while

(d) all of the above

5) Primitive data types are also known as

(a) Derived data types

(b) Reference data types

(c) User defined data types

(d) Intrinsic data types

6) The size of char data type is _______

(a) 2 bytes

(b) 4 bytes

(c) 6 bytes

(d) 8 bytes

7) The size of double data type is _______

(a) 2 bytes

(b) 4 bytes

(c) 6 bytes

(d) 8 bytes

8) Which of the following is / are valid escape sequences in Java

i ) \ n    ii) \t    iii) \?    iv) \p

(a) i) and ii)

(b) i) and iii)

(c) i) and iv)

(d) all of the above

9) Which of the following is / are valid identifier(s) in Java

i ) _new    ii) new   iii) New_    iv) 1new

(a) i) and ii)

(b) i) and iii)

(c) i) and iv)

(d) all of the above

10) Class variables are also known as ________

(a) Local variable

(b) Instance variable

(c) non static variable

(d) static variable

Question 2) State True Or False.

1) A character is written within double quotes.

(a) True

(b) False

2) \t is a special printing character used for printing horizontal tab

(a) True

(b) False

3) A string is enclosed in double quotes

(a) True

(b) False

4) Arrays and Objects are non-primitive data types in Java.

(a) True

(b) False

5) Identifiers mus be a keywords or boolean literal or null literal.

(a) True

(b) False

Question 3) Find odd one out.

1) 

(a) byte

(b) short

(c) int

(d) float

2) 

(a) int

(b) boolean

(c) String

(d) char

3) 

(a) \n

(b) \t

(c) \p

(d) \v

4)

(a) { }

(b) [ ]

(c) < >

(d) ( )

5) 

(a) Array

(b) Interface

(c) Class

(d) int

Chapter 2 Elementary Concepts Of Objects and Classes

 Chapter 2

 Elementary Concepts Of Objects and Classes

Question 1 ) Choose the correct option from the following. 

1) A class is a collection of _______

(a) data

(b) objects

(c) methods

(d) variables

2) A __________ is a user defined aggregate data type.

(a) variable

(b) class

(c) method

(d) none of the above

3) The objects belonging to a class are called

(a) data

(b) state

(c) instance

(d) members

4) A class  attributes are defined by _______

(a) variables

(b) class

(c) method

(d) none of the above

5) Behavior of a class of object is implemented using _______

(a) variable

(b) class

(c) method

(d) none of the above

Question 2) State True Or False.

1) Every object created from the same class will have different features.

(a) True

(b) False

2) Class is a factory of objects.

(a) True

(b) False

3) State and behavior of the objects are interlinked.

(a) True

(b) False

4) A class can also have its sub-class

(a) True

(b) False

5) A Java file have only one non-public class.

(a) True

(b) False