Subscribe Us

Showing posts with label Programming idea. Show all posts
Showing posts with label Programming idea. Show all posts

C Array




In C, array is a composite data type. Because we can create an array with the help of primitive data type like int, chat, double etc.

Syntax for creation of one Dimensional array in C

int a[5];

Here a is the array name, type is int. total allocation is 5(5*4=20bytes) here index starts from 0(zero)

Initialization of an array :

int a[]={1,2,3,4,5,6}

if you want access element from above mentioned array, then you can access it with help of index. for example the index of value 3 is 2.

if you want to access all the element from an array, then you can use loop

example : for loop

for(i=0;i<6;i++)

{

   printf("%d\n", a[i]);

}

to calculate size of an array, use the formula :

l=sizeof(a)/sizeof(a[0])


Question & Answer


Choose correct or the best alternative in the following:

Q.1 What is the output of the following program?

main ( )

{ int x = 2, y = 5;

if (x < y) return (x = x+y); else printf (“z1”);

printf(“z2”);

}

(A) z2 (B) z1z2

(C) Compilation error (D) None of these

Ans: D

There is no compilation error but there will no output because function is returning a

value and if statement is true in this case.

Q.2 Choose the correct one

(A) Address operator can not be applied to register variables

(B) Address operator can be applied to register variables

(C) Use of register declaration will increase the execution time

(D) None of the above

Ans: D

A register access is much faster than a memory access, keeping the frequently

accessed variables in the register will lead to faster execution of programs.

Q.3 What is the following program doing?

main ()

{ int d = 1;

do

printf(“%d\n”, d++);

while (d < = 9);}

(A) Adding 9 integers (B) Adding integers from 1 to 9

(C) Displaying integers from 1 to 9 (D) None of these

Ans: C

d starting from 1 is incrementing one by one till d=9 so the printf statement is printing

numbers from 1 to 9.

Q.4 What is the output of the following program?

main ( )

{ extern int x;

x = 20;

printf(“\n%d”, x);

}


(A) 0 (B) 20

(C) error (D) garbage value

Ans: C

Output of the given program will be “Linker error-undefined symbol x”. External

variables are declared outside a function.

Q.5 If x is one dimensional array, then pick up the correct answer

(A) *(x + i) is same as &x[i] (B) *&x[i] is same as x + i

(C) *(x + i) is same as x[i] +1 (D) *(x + i) is same as *x[i]

Ans: A

num[i] is same as *(num+i)

Q.6 Consider the following declaration

int a, *b = &a, **c = &b;

The following program fragment

a = 4;

**c = 5;

(A) does not change the value of a (B) assigns address of c to a

(C) assigns the value of b to a (D) assigns 5 to a

Ans: D

The given statements assigns 5 to a

Q.7 Choose the correct answer

(A) enum variable can not be assigned new values

(B) enum variable can be compared

(C) enumeration feature increase the power of C

(D) None of the above

Ans: C

The enumerated data types give an opportunity to invent our own data typeand define

what value the variable of this data type can take.

Q.8 The content of file will be lost if it is opened in

(A) w mode (B) w+ mode

(C) a mode (D) a+ mode

Ans: A

When the mode is writing, the contents are deleted and the file is opened as a new file.

Q.9 Consider the following code segment:

int a[10], *p1, *p2;

p1 = &a[4];

p2 = &a[6];

Which of the following statements is incorrect w.r.t. pointers?

(A) p1 + 2 (B) p2 – 2

(C) p2 + p1 (D) p2 – p1

Ans: C


Addition of two pointers is not allowed.

Q.10 The second expression (j – k) in the following expression will be evaluated

(i + 5) && (j – k)

(A) if expression (i + 5) is true.

(B) if expression (i + 5) is false.

(C) irrespective of whether (i + 5) is true or false.

(D) will not be evaluated in any case.

Ans: A

In a compound logical expression combined with &&, the second expression is

evaluated only if first is evaluated in true.


In the for statement: for (exp1; exp2; exp3) { … }

where exp1, exp2 and exp3 are expressions. What is optional?

(A) None of the expressions is optional.

(B) Only exp1 is optional.

(C) Only exp1 and exp3 are optional.

(D) All the expressions are optional.

Ans: D

All the expressions are optional. For (;;) is a valid statement in C.

Q.12 The output of the following code segment will be

char x = ‘B’;

switch (x) {

case ‘A’: printf(“a”);

case ‘B’: printf(“b”);

case ‘C’: printf(“c”);

}

(A) B (B) b

(C) BC (D) bc

Ans: D

Since there is no break statement, all the statement after case’B’ are executed.

Q.13 What will be the output of the following code segment?

main( ) {

char s[10];

strcpy(s, “abc”);

printf(“%d %d”, strlen(s), sizeof(s));

}

(A) 3 10 (B) 3 3

(C) 10 3 (D) 10 10

Ans: A

strlen(s) give the length of the string, that is 3 and sizeof(s) give the size of array s

that is 10.

Q.14 Which of the following is the odd one out?

(A) j = j + 1; (B) j =+ 1;

(C) j++; (D) j += 1;

Ans: B

j=+1 is odd one out as rest all means incrementing the value of variable by 1.

Q.15 Which of the following is true for the statement:

NurseryLand.Nursery.Students = 10;

(A) The structure Students is nested within the structure Nursery.

(B) The structure NurseryLand is nested within the structure Nursery.

(C) The structure Nursery is nested within the structure NurseryLand.

(D) The structure Nursery is nested within the structure Students.

Ans: C

The structure Nursery is nested within the structure NurseryLand.

Q.16 What will be the output of the following code segment, if any?

myfunc ( struct test t) {

strcpy(t.s, “world”);

}

main( ) {

struct test { char s[10]; } t;

strcpy(t.s, “Hello”);

printf(“%s”, t.s);

myfunc(t);

printf(“%s”, t.s);

}

(A) Hello Hello (B) world world

(C) Hello world (D) the program will not compile

Ans: D

The program will not compile because undefined symbol s for myfunc( ) function.

Structure should be defined before the main and the function where it is called.

Q.17 If a function is declared as void fn(int *p), then which of the following statements is

valid to call function fn?

(A) fn(x) where x is defined as int x;

(B) fn(x) where x is defined as int *x;

(C) fn(&x) where x is defined as int *x;

(D) fn(*x) where x is defined as int *x;

Ans: B

Function void fn(int *p) needs pointer to int as argument. When x is defined as int

*x, then x is pointer to integer and not *x.

Q.18 What is the following function computing? Assume a and b are positive integers.

int fn( int a, int b) {

if (b == 0)

return b;

else

return (a * fn(a, b - 1));

}

(A) Output will be 0 always (B) Output will always be b

(C) Computing ab (D) Computing a + b

Ans: A

The output is always be 0 because b is decremented in recursive function fn each time

by 1 till the terminating condition b==0 where it will return 0.

Q.19 What is the output of the following C program?

# include <stdio.h>

main ( )

{

int a, b=0;

static int c [10]={1,2,3,4,5,6,7,8,9,0};

for (a=0; a<10;+ + a)

if ((c[a]%2)= = 0) b+ = c [a];

printf (“%d”, b);

}

(A) 20 (B) 25

(C) 45 (D) 90

Ans: A

printf statement will print b which is sum of the those values from array c which get

divided by 2, that is 2+4+6+8=20.

Q.20 If a, b and c are integer variables with the values a=8, b=3 and c=-5. Then what is the

value of the arithmetic expression:

2 * b + 3 * (a-c)

(A) 45 (B) 6

(C) -16 (D) -1

Ans: A

the value of the arithmetic expression is 45 as 2*3+3*(8—5)=6+3*13=6+39=45

Thank you, Keep Learning

Share:

R Vector



In R, Vector is a collection of same data type just like one Dimensional array in C.

we can create vector as follows 

v=c()

or 

v=c(1,2,3,4,5)

first statement is the creation of empty vector whereas second statement is the creation of the vector with some value.

to calculate length of vector we can use length function like 

x=length(v)

To Access any element from vector, we can use index position, here index position starts from 1 (where as in c Array index position starts from 0)

i.e, if you want to access 4 from above vector, then you can write x=v[4]

To access all the element from the vector 

x = c(1,3,4,7)

n = length(x)

for(i in 1:n)

{

print(x[i])

}

Thank You , Keep Learning

Share:

Java String




In java, String is a final class. which is inherit from object class.  

we can use final keyword against variable, function and class. if you use final keyword against a variable, then the variable will be constant. 

if you use final keyword against class, then it can not be inherited, and if you use it against function , then you cannot be overridden the function. 

So from above discussion, we can say String is a class which cannot be inherited .

a) Creation of  String :

String s= "hello"

b) Calculate length of the String 

int l= s.length();

c) Access the element with respect of index:

char c= s.charAt(i), where i represents the index

if you want access all the element from a string then we can use either use for loop or for-each loop

ex. for loop

for(i=0;i<s.length();i++)

{

 System.out.print(s.charAt(i));

}

d) Convert String to Character Array

Char t[]=s.tocharArray();

Thank you, keep learning

Share:

Python List

 



What is List : 

In python, list is a collection of any data type which is mutable, i.e means we can change the value of list  

list Creation

we can create list in two ways

a) Empty List : L=[]

b) With some values : L1=[1,2,3,4,5,6]

After creation of empty list we can add some number with the help of append and extend function. 

for accessing list element we can use the index of the element. i.e if we want to access 3 from above mentioned list(L1), then we have to write, x= L1[2],  where 2 is the index of 3.

if you want to access all the element, then you can use range function as follows:

for i in range (0,len(L1)):

    print(L1[i])

Here, i represents the index of individual element. 

Lists are pretty useful, but only if you can get at the things inside them. You can already go through the elements of a list in order, but what if you want, say, the fifth element? You need to know how to access the elements of a list. Here’s how you would access the first element of a list:

animals = ['bear', 'tiger', 'penguin', 'zebra']

bear = animals[0]


How do you make a two- dimensional (2D) list?

That’s a list in a list like this: [[1,2,3],[4,5,6]].

Aren’t lists and arrays the same thing?

It depends on the language and the implementation. In classic terms, lists are very different from arrays because of how they’re implemented. In Ruby, lists are referred to as arrays. In Python, they’re referred to as lists. Just call these lists for now, since that’s what Python calls them. 

How come a for- loop can use variables that aren’t defined yet?

It defines that variable, initializing it to the current element of the loop iteration, each time through. 

Why does for i in range(1, 3): only loop two times instead of three times?

The range() function only does numbers from the first to the last, not including the last. So itstops at two, not three, in the above. This turns out to be the most common way to do this kind of loop.

What does elements.append() do?

It simply appends to the end of the list. Open up the Python shell and try a few examples with a list you make. Any time you run into things like this, always try to play with them interactively in the Python shell.

What is While loop

A while- loop will keep executing the code block under it as long as a boolean expression is True. they do is simply do a test like an if- statement, but instead  of  running the code block once, they jump back to the “top” where the while is and repeat. It keeps doing this until the expression is False. Here’s the problem with while- loops: Sometimes they do not stop. This is great if your intention is to just keep looping until the end of the universe. Otherwise you almost always want your loops to end eventually. To avoid these problems, there’s some rules to follow:

1. Make sure that you use while- loops sparingly. Usually a for- loop is better.

2. Review your while statements and make sure that the thing you are testing will become False at some point.

3. When in doubt, print out your test variable at the top and bottom of the while- loop to see what it’s doing.


String Delimiters, Part I

A string in Python is a sequence of characters. For Python to recognize a sequence of characters, like hello, as a

string, it must be enclosed in quotes to delimit the string.

For this whole section on strings, continue trying each set-off line of code in the Shell. Try

"hello"

Note that the interpreter gives back the string with single quotes. Python does not care what system you use. Try

'Hi!'

Having the choice of delimiters can be handy.

Figure out how to give Python the string containing the text: I’m happy. Try it. If you got an error, try it with another type of quotes, and figure out why that one works and not the first.

There are many variations on delimiting strings and embedding special symbols. 

Note: A string can have any number of characters in it, including 0. The empty string is ’’ (two quote characters

with nothing between them). Many beginners forget that having no characters in the middle is legal. It can be useful.

Strings are a new Python type. Try

type('dog')

type('7')

type(7)

The last two lines show how easily you can get confused! Strings can include any characters, including digits. Quotes

turn even digits into strings.

String Concatenation

Strings also have operation symbols. Try in the Shell (noting the space after very):

'very ' + 'hot'

The plus operation with strings means concatenate the strings. Python looks at the type of operands before deciding

what operation is associated with the +. Think of the relation of addition and multiplication of integers, and then guess the meaning of

3*'very ' + 'hot'

Were you right? The ability to repeat yourself easily can be handy. Predict the following and then test. Remember the last section on types:

7+2

'7'+'2'

Python checks the types and interprets the plus symbol based on the type. Try

'7'+2

With mixed string and int types, Python sees an ambiguous expression, and does not guess which you want - it just gives an error! 

This is a traceback error. These occur when the code is being executed. In the last two lines of the traceback it shows the Python line where the error was found, and  then a reason for the error. Not all reasons are immediately intelligible to a starting programmer, but they are certainly worth checking out. In this case it is pretty direct. You need to make an explicit conversion, so both are strings if you mean concatenation, ’7’ + str(2), or so both are int if you mean addition, int(’7’) + 2.



Share:
Powered by Blogger.

Ad Code

Responsive Advertisement

Ad Code

Responsive Advertisement

Featured post

Search This Blog

Recently added book names

THE HTML AND CSS WORKSHOP   | MICROSOFT POWER BI COOKBOOK   | MongoDB in Action, 2nd Edition  | ADVANCED DEEP LEARNING WITH PYTHON   | Cracking Codes with Python An Introduction to Building and Breaking  | Moris Mano Degital Design 3rd Edition  | Beginning App Development with Flutter by Rap Payne  |react hooks in Action - John Larsen   | Artificial Intelligence A Modern Approach Third Edition Stuart Russel  | Data Structures and Algorithms - Narasimha Karumanchi   | Thomas S.M. - PostgreSQL High Availability Cookbook - 2017  | Gunnard Engebreth PHP 8 Revealed Use Attributes the JIT Compiler   | ICSE Class X Computer Application Notes   | INTERNET OF THINGS PROJECTS WITH ESP32   | 100 aptitude trick(102pgs)s   | OBJECT_ORIENTED_PROGRAMMING Question & Answer   | C questions and answer   | Full_Book_Python_Data_Structures_And_Algorithm   | Jira 8 Administration Cookbook Third Edition  | KALI LINUX WIRELESS PENETRATION TESTING BEGINNERS GUIDE THIRD EDITION - Cameron Buchanan, Vivek Ramachandran  HTML5 & javascript By :- Jeanine Meyer   | Python For Beginners Ride The Wave Of Artificial Intelligence   | HackingTheXbox   | Introduction to Algorithms 3rd.Edition - (CLRS)   | The C++ Programming Language - Bjarne Stroustrup   | Modern C++ Programming Cookbook - Marius Bancila   | Java The Complete Reference Eleventh Edition   Data_Communications and Networking 4th Ed Behrouz A Forouzan   | DevOps with Kubernetes - Hideto Saito   | The-Linux-Command-Line-A-Complete-Introduction   | Assembly Language for X86 Processors KIP R. Irvine   | Effective_Modern_C++ - Scott Meyer

Contact Form

Name

Email *

Message *

Followers

Mobile Logo Settings

Mobile Logo Settings
image

Computer Training School Regd. under Govt. of West Bengal Society Act 1961

Header Ads Widget

Responsive Advertisement

Hot Widget

random/hot-posts

Recent in Sports

Popular Posts

Most Popular

Popular Posts

Labels

Blogger templates