Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

TEMPLATE FOR ASSESSMENTS

1. Read Guidelines – in case of any doubt check with your mentor.


2. The final submission will have to be in soft copy in MS word as per template shared below.
3. Use Calibri font size 9
4. Keep Questions short and crisp. Word count should not exceed 20 words for questions
and 8 words for options.
5. In the last row – mention the correct option as a) or b)
6. The Blooms level has been fixed – so please design question accordingly.
7. The rows heights have been fixed, so that the table size is not changed. If you have any
problem, use this link to learn how to fix it YouTube
Insert the exact details within the < >
<CO, IF>: <22412>: < Java Programming >: < Basic Syntactical Constructs in Java >: <LO3>: <Assessments>:
<Formative>
<Yogita Jore>
Assessment Type: Formative Assessments: Embedded questions in video

Set 1: Question No 1 Set 1: Question No 2 Set 1: Question No 3

The Java _______ statement executes What is the main difference between a What is the output of the below Java
one statement from multiple conditions. WHILE and a DO-WHILE loop in Java? program with FOR loop?
for(int j=0; j<5;j++;)
System.out.print(j + ",");

Recall/ Remembering Understanding Application


a) switch a) WHILE loop executes the statements a) 1,2,3,4
inside of it at least once even if the
condition is false.
b) if b) DO-WHILE loop executes the b) 0,1,2,3,4
statements inside of it at least once even
if the condition is false.

c) for c) WHILE loop is fast. c) Compile error


d) while d) DO-WHILE loop is fast. d) 1,2,3,4,5
Ans: < switch > Ans: < DO-WHILE loop executes the Ans: < Compile error >
statements inside of it at least once even
if the condition is false. >

Set 2: Question No 1 Set 2: Question No 2 Set 2: Question No 3


Choose the correct syntax of the DO ______ jump statements can skip int a=1;
WHILE loop in Java below. processing the remainder of the code in while(a<4)
its body for a particular iteration. {
System.out.print(a + " ");
a++;
}
Identify the output of above code.

Recall/ Remembering Understanding Application


a) do a) break a) 1 2 3 4
{
//statements
}while(condition);
b) do b) return b) 1 2 3
{
//statements
}while(condition)

c) do while(condition) c) exit c) 6
{
//statements
}
d) while(condition) d) continue d) 1 2 3 4 5
{
//statements
} do

Ans: < do Ans: < continue > Ans: <1 2 3 >


{
//statements
}while(condition);>
Assessment Type: Summative: End of LO: in LMS

Summative: Q 1 Summative: Q 2 Summative: Q 3 Summative: Q 4 Summative: Q 5

Which of these An Enhanced FOR loop in public class temp Initialization, public class demo
selection statements Java misses ___ and __ { Increment/Decrement {
test only for equality? compared to the old- public static void public static void
style FOR loop. main(String agrs[]) main(String[] args)
{ {
for(int i=1; i<=10; i++); for(int i = 0; i<5; i++)
System.out.print(i); {
}
} System.out.println("He
llo");
break;
}
}
}
Analyse the output of
above code.
Recall/ Remembering Understanding Application Understanding Application

a) if a) Speed and Easiness a) 12345678910 a) a) Hello

b) switch b) Initialization, b) 11 b) b) Hello


Increment/Decrement Hello
Hello
Hello
Hello
c) for loop c) Semicolons, Variables c) Error c) c) Hello
Hello
Hello
Hello

d) continue d) comparison d) 1 2 3 4 5 6 7 8 9 10 d) d) Error

Ans: < switch > Ans: < Initialization, Ans: <Error > Ans: < > Ans: < Hello>
Increment/Decrement >
Assessment Type: Practice Worksheets: End of LO: in LMS/ downloadable PDF

A.Write a program to print prime numbers in the given range.


class primenum
{
public static void main(String args[])
{
int i,num=0,n=20;
System.out.println("value of n="+n);
while(num<=n)
{
i=2;
while(i<=num)
{
if(num%i==0)
break;
i++;
}
if(i==num)
System.out.print(num +” “);
num++;
}
}
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

B.Write a program to display Multiplication Table.


class multi
{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]);
System.out.println("Multiplication Table");
for(int i=1; i<=num;i++)
{
for(int j=1;j<=num;j++)
{
System.out.print(" "+i*j+" ");
}
System.out.print("\n");
}
}
}
C.Write a program to find sum of digits of number. (input number=321 sum=3+2+1=6)
A.
import java.lang.*;
import java.io.*;
class sumdigit
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
int number, sum=0;
try
{
System.out.print("Enter Number:");
number=Integer.parseInt(in.readLine());
while(number>0)
{
sum=sum+number%10;
number=number/10;
}
System.out.println("Sum of digit is="+sum);
}
catch(Exception e)
{
System.out.println("Error");
}
}
}

D. Difference between for loop and for each loop.


B.
The for loop is best for iterating over name-value pairs, and the forEach is loop best for iterating
over values, for example arrays or objects.
The for loop is best for name-value pairs.The for loop can be used to explore the possible
properties and methods of an object.
The forEach loop is best for iterating values. Ex: arrays, and objects.
forEach is a method on the Array prototype. It iterates through each element of an array and
passes it to a callback function.
A for statement is more flexible as it does not necessarily involve an array.
The performance of a normal for loop is slightly better, because there is no function call for each
element involved.
E. Explain switch case with example.
The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The
switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and
Long. Since Java 7, you can use strings in the switch statement.
Syntax:
switch(expression)
{    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    
    
default:     
 code to be executed if all cases are not matched;    
}
Example:
public class Test {
    public static void main(String[] args)
    {
        int day = 5;
        String dayString;
 
        // switch statement with int data type
        switch (day) {
        case 1:
            dayString = "Monday";
            break;
        case 2:
            dayString = "Tuesday";
            break;
        case 3:
            dayString = "Wednesday";
            break;
        case 4:
            dayString = "Thursday";
            break;
        case 5:
            dayString = "Friday";
            break;
        case 6:
            dayString = "Saturday";
            break;
        case 7:
            dayString = "Sunday";
            break;
        default:
            dayString = "Invalid day";
        }
        System.out.println(dayString);
    }
}
E. Difference between while and do while loop.

BASIS OF
WHILE LOOP DO-WHILE LOOP  
COMPARISON
The main feature of the while loop is that The main feature of the do while is an exit
Main Feature
it’s an entry controlled loop. controlled loop.  
Given that the condition is checked first, Given that the condition is checked later,
Statements statements may or may not get the body statements will execute at least
executed.     once.
The iterations do not occur if the In Do-while loop the iteration occurs at least
Iterations condition at the first iteration appears once even if the condition is false at the first
false.   iteration.
Semicolon Semicolon (;) is not used in while loop.     Semicolon (;) is used in Do-while loop.
In while loop, the controlling condition In Do-while loop the controlling condition
Controlling Condition
appears at the start of the loop.   appears at the end of the loop.
The code is short and therefore it takes The code is relatively long and therefore it
Nature
much less time to execute.   takes extra time to execute.

You might also like