0% found this document useful (0 votes)
7 views

TillSimpleLoops_2024

Uploaded by

nandu.devu2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

TillSimpleLoops_2024

Uploaded by

nandu.devu2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Total – 80M SECTION – A ( 21 M) Time : 1.

5 hour

Question 1: Choose the correct answer and write the correct option (1M * 20)

1. If X = 5, find its value after the following statement X* =X++ % 2


(a) 0 (b) 5 (c) 6 (d) 10
2. Non-Printable characters that hold a special meaning
(a) operators (b) backslash (c) escape sequence (d) keywords
3. Keyword to differentiate between variable and a constant
(a) const (b) static (c) default (d) final
4. Name oops concepts implemented : Accessing data members through member functions.
(a) encapsulation (b) abstraction (c) polymorphism (d) inheritance
5. Which of the following is valid for variable naming
(a) roll no 1 (b) rollno1 (c) roll+no1 (d) 1rollno
6. Which of the following is a valid separator
(a) ? (b) : (c) {} (d) >
7. What will be the value in ch if int x = 70; char ch = x++;
(a) compile time error (b) runtime error (c) F (d) G
8. Different objects of a class have common?
(a) characteristics (b) abstraction (c) behavior d) inheritance
9. The termination of each statement must be indicated with a ?
(a) : (b) . (c) , (d) ;
10. Which OOPS principle does below represents:

(a) abstraction (b) encapsulation (c) polymorphism (d) inheritance


11. Which of the following is a Boolean literal
(a) ‘true’ (b) “true” (c) “false” (d) false
12. What will be the value in str:
String str=1+ 'A' + " Bla Bla " + 20;
(a) 1ABlaBla20 (b) 1A BlaBla 20 (c) 66 Bla Bla 20 (d) error
13. The statement for(i=1; i<=10 ; i++) ; signifies ?
(a) step loop (b) infinite loop (c) delay loop (d) continuous loop
14. Output of statement Math.pow(25,1/2) + Math.sqrt(256) is
(a) 21.0 (b) 16.0 (c) 21 (d) 16
15. Name the type of error in the statement int x = Math.pow(10,2);
(a) syntax (b) logical (c) runtime (d) none of the above
16. Which of the following is the correct precedence of logical operators
a. ! && || b. && ! || c. || ! && d. && || !
17. Which of the following loop does execute at least once if the condition is false in the
beginning
(a) do-while (b) while (c) for (d) switch
18. An if statement inside another if statement is called as
(a) internal if (b) nested if (c) if-else (d) if and only if
19. Which of these selection statements test only for equality
(a) if (b) if and switch (c) switch (d) loop
20. What will be the output if int a=15, b= 25;
if(a < b || (a=5) > 15 ) System.out.println(a);
else System.out.println(a);
15
Question 2: Solve the following (2M * 15)
10
𝑥 3/2𝑦1/2
1. Write the java expression for ( )
𝑥 1/4

Math.pow( (Math.pow(x , 3.0/2) * Math.pow(y , 1.0/2) ) / Math.pow(x , 1.0/4) , 10);


2. Differentiate between a = b and a == b

A=b A==b
It is an assignment operator It is an equality operator
It is used to assign value from b to a It is used to compare if values in a and b are
exactly equal or not

3. Differentiate between entry controlled loop and exit controlled loop with example.

Entry Exit
It checks the condition at the time of entry. It checks the condition after executing its
Only if the condition is true, the program body. If the condition is true, loop will
control enters the body of the loop. perform the next iteration otherwise program
control will move out of the loop.
Loop does not execute at all if the condition is The loop executes at least once even if the
false. condition is false.
Ex: for, while Ex: do-while

4. Explain what is fall through in java with an example.

break statement at the end of case is optional. Omitting break leads to program execution
continuing into the next case and onwards till a break statement is encountered or end of switch is
reached. This is termed as Fall Through in switch case statement.

5. Explain the function of break and continue with an example:

break statement is used to unconditionally jump out of the loop whereas continue statement is
used to unconditionally jump to the next iteration of the loop, skipping the remaining statements
of the current iteration.

break statement is used in switch-case and loops whereas continue statement is only used in
loops.

6. Write the output of the following program code if char ch ; int x=97;
do {
ch=(char) x;
System.out.print(ch + “ ” );
if(x%10 == 0)
break;
++x;
}while(x<=100);

abcd
7. Convert the following while loops into for loops. Also write the output of the program
int i, x = 0;
while (i<=20){
System.out.println(i+ “ ”);
x++;}

int i, x = 0;
for (; i<=20 ; x++){
System.out.println(i+ “ ”);
x++;
}
compile time error

8. Explain token and keywords

Tokens are individual components of a java statement that carry out certain functions and
participate in the program. Ex: variable, keyword etc

Java keywords or reserved words are the words which carry a special meaning for the system
compiler. These words are basically used for writing a Java statement in the program. Such words
cannot be used for naming a variable in the program.

Some keywords are case, switch, int, float etc.

9. Differentiate static and dynamic initialization in java

In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example:

int mathScore = 100;


double p = 1.4142135;
char ch = 'A';

In dynamic initialization, the initial value of the variable is the result of an expression or the
return value of a method call. Dynamic initialization happens at runtime. For example:

int a = 4;
int b = Math.sqrt(a);

double x = 3.14159, y = 1.4142135;


double z = x + y;

10. What are pure and impure expressions?

In pure expressions, all the operands are of same type. The result is the same type as the values
Ex: int x =1, y=2, z= 3;
int s = x+y+z

In mixed expressions, the operands are of mixed or different data types. The result is of the higher
data types
Ex: double s = 2+’a’+3.0;
11. Convert the following to if-else:
int x = a % 2 == 0 ? 1 : a%3 ==0 ? 0 : -1

int x;
if(a%2==0) x=1;
else if(a%3==0) x=0;
else x=-1;

12. What are literals. Explain null literal.


Literal is a constant that remains fixed throughout the discussion of a program. It is the value that
can be assigned to a variable.
The null in Java is a literal of the null type. It is used to initialize an object or reference variable

13. Explain why java is compiled and interpreted language


In Java compilation process, the source code of a Java program is compiled to .class file an
intermediate binary code called the Bytecode if no syntax errors. This Bytecode is interpreted and
executed by JVM on the target platform.

14. Define comments and mention the types of comments along with the syntax

Java comments are notes in the source code of a Java program that are ignored by the compiler.
They are used to provide explanations, descriptions, or any relevant information about the code. It
makes it easier to understand for humans.
Types of comments:
Single line : //
Multi Line : /*. */
Documentaiton : /** */
15. Name the following:
a. One thing in many different forms : polymorphism
b. Method that returns the largest double value that is less than or equal to the argument
and is equal to a mathematical integer : Math.floor()
c. The keyword in a switch case, when the switch value does not match with any case
default
d. Two or more statements that can be grouped together by enclosing them between
opening and closing curly braces : compound statement

SECTION B [ Provide Variable Listing ] (30 M)

1) Using the switch statement, write a menu driven program to perform following operations:

(i) To Print the value of Z where Z =(x3+0.5x)/Y


where x ranges from –10 to 10 with an increment of 2 and Y remains constant at 5.5.

(ii) To print the sum of following series


s = 3/a4 + 6/a8 + 9/a4 + 12/a4 + ….. n
import java.util.Scanner;

public class SwitchMenu


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Value of Z");
System.out.println("Type 2 for series");
System.out.print("Enter your choice: ");

int choice = in.nextInt();

switch (choice) {
case 1:
double Z, Y = 5.5;
for (int x = -10; x <= 10; x+= 2) {
Z = (Math.pow(x, 3) + 0.5 * x) / Y;
System.out.println("Value of Z when x is "
+ x + " = " + Z);
}
break;

case 2:
System.out.print("Enter number of terms: ");
int N = in.nextInt();
System.out.print("Enter a: ");
double a = in.nextDouble();

double num = 3, den = 4, sum = 0;


for (int i = 1; i <= N+; i++) {
num = num*i;
den = den * i;
sum = sum + num / Math.pow(a, den);
}
System.out.println(sum);
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}

2) Write a program to take a n-digit number as input. Find if the number is Armstrong or not.
Also check if number is palindrome or not and print output as follows:
Ex: 153 Output: Number is Armstrong but not palindrome
Ex: 121 Output: Number is not Armstrong but palindrome
Ex: 132 Output: Number is neither Armstrong nor palindrome

import java.util.Scanner;
public class Armstrong {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);


System.out.println("Enter no");

int number = in.nextInt();


int originalNumber, rev = 0,c= 0,result = 0, remainder;

originalNumber = number;
while (originalNumber != 0)
{
c++;
originalNumber /= 10;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, c);
originalNumber /= 10;
}

originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
rev = rev * 10 + remainder;
originalNumber /= 10;
}

if(result == number && rev != number)


System.out.println(number + " is an Armstrong number and not
palindrome");
else if(result != number && rev == number)
System.out.println(number + " is not an Armstrong number but
palindrome");
else
System.out.println(number + " is neither Armstrong nor palindrome");

}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy