Interviews39 answers
What is the output of following. var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc"; ?
39 Answers
var a = 5;
a = a + "abc"; //Error
dynamic b = 5;
b = b + "abc";
Rashid MuhammadPosted Jan 23, 2018, 7:51 PM EST
var a = 5;
a = a+"abc"; //Compile time error (cannot implicitly convert string to int)
dynamic b = 5;
b = b+"abc";
Console.WriteLine(b);// 5abc
krishan kumar singhPosted Oct 11, 2017, 11:48 AM EDT
a=a+"abc"; throws a compile time error, cant convert int to string type,
b=b+"abc"; returns ==> 5abc;
Muneer LeoPosted Jan 10, 2017, 1:58 AM EST
var: It is a compile time entity
dynamic : it is a run time entity
var a = 5;
a = a+"abc";
Ans: compile time error, here a is integer datatype and trying to do addition operation with string datatype. so we cannot add integer with string
dynamic b = 5;
b = b+"abc";
Ans: at first b is a integer, then to do operation for adding with string, here dynamic will convert b integer data type to string data type then concatenate the both value.
Mohan KPosted Nov 30, 2016, 2:54 AM EST
var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc";
In the var a = 5 // assigned integer value 5 to var so data type will be int as var is early bonded so it will decide on compile that what the data type of value is assign.
so when a = a+"abc"; will be execute it will give the conversion error.
manisha vermaPosted Nov 14, 2016, 11:45 AM EST
var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc";
In the var a = 5 // assigned integer value 5 to var so data type will be int as var is early bonded so it will decide on compile that what the data type of value is assign.
so when a = a+"abc"; will be execute it will give the conversion error.
manisha vermaPosted Nov 14, 2016, 11:45 AM EST
var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc";
In the var a = 5 // assigned integer value 5 to var so data type will be int as var is early bonded so it will decide on compile that what the data type of value is assign.
so when a = a+"abc"; will be execute it will give the conversion error.
manisha vermaPosted Nov 14, 2016, 11:45 AM EST
Cannot implicitly convert type 'string' to 'int' error will display
Naveen BishtPosted Jul 18, 2016, 1:51 AM EDT
fd
Suresh MolabantiPosted Jun 15, 2016, 6:00 AM EDT
fsdsf
Suresh MolabantiPosted Jun 15, 2016, 6:00 AM EDT
ar a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
but it is possible
var a = "5";
a = Convert.ToInt32(a)+ "abc";
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 6:00 AM EDT
var a = "5";
a = Convert.ToInt32(a)+ "abc";
Suresh MolabantiPosted Jun 15, 2016, 5:59 AM EDT
var a = "5";
a = Convert.ToInt32(a)+ "abc";
Suresh MolabantiPosted Jun 15, 2016, 5:59 AM EDT
var a = "5";
a = Convert.ToInt32(a)+ "abc";
Suresh MolabantiPosted Jun 15, 2016, 5:59 AM EDT
var a = "5";
a = Convert.ToInt32(a)+ "abc";
Suresh MolabantiPosted Jun 15, 2016, 5:59 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
var a = 5; a = a+"abc" it gives compilation error can't convert string to int.
why because 5 is an integer.int+string is not valid.that's why error is coming.
Console.WriteLine("Enter a number");
var b = Console.ReadLine();------->6
b = b + "abc";
Console.WriteLine(b); Answer is:6abc
here,enter dynamic value is treated as string.
Suresh MolabantiPosted Jun 15, 2016, 5:55 AM EDT
a=error (Error Cannot implicitly convert type 'string' to 'int')
b=5abc
Chetan RaiyaniPosted Jun 7, 2016, 11:18 PM EDT
var a = 5; a = a+"abc" - Error [var - Compile Time assignment]
dynamic b = 5; b = b+"abc" - 5abc [Dynamic is Run Time]
Bhuvanesh MohankumarPosted May 29, 2016, 7:26 AM EDT
1) Error
2) 5abc
Vivek KumarPosted Apr 17, 2016, 1:55 AM EDT
output would be 5abc
Keerthi VenkatesanPosted Apr 11, 2016, 5:36 AM EDT
5abc will be the output
Amatya AgyeyPosted Mar 29, 2016, 8:16 PM EDT
1)Error
2)5abc
Sreekanth ReddyPosted Mar 15, 2016, 1:17 AM EDT
var is statically typed and dynamic is dynamically typed.
var needs to initialize at the time of declaration and cannot be changed. Since the compiler has already decided the type var variable we cannot assign other values to var variable it violates the type safety and compile time error will throw.But on other hand we can declare dynamic variables and later we can assign any values to it
vipin kvPosted Mar 10, 2016, 3:37 AM EST
First Expression will throw an compile time error related to conversion of string to int and second expression will run successfully and give a result like "5abc" because type for dynamic variable is decided at run time so in this expression b+"abc" = 5+"abc" will execute as concatenation of two string...
Pankaj Kumar ChoudharyPosted Feb 9, 2016, 4:05 PM EST
var a=5;a=a+"abc" ; will give compile time error as var is implicitly typed local variable and it takes the datatype of the value that is initialized to it .Now when you try to assign any other type value to it ,it will throw compile time error.In case of dynamic b=5;b=b+"abc"; It will run as dynamic types takes the data type of whatever you assign to it and you can also change its type at runtime.
Rajeev PunhaniPosted Jan 27, 2016, 7:41 AM EST
var a=5;a=a+"abc" ; will give compile time error as var is implicitly typed local variable and it takes the datatype of the value that is initialized to it .Now when you try to assign any other type value to it ,it will throw compile time error.In case of dynamic b=5;b=b+"abc"; It will run as dynamic types takes the data type of whatever you assign to it and you can also change its type at runtime.
Rajeev PunhaniPosted Jan 27, 2016, 7:41 AM EST
Both case will execute successfully & will give the same output - 5abc.
Sujeet SumanPosted Jan 26, 2016, 11:51 PM EST
In the first case, there would be a compiler error. Because a is of type integer, and thus it won't work when you add integer and string.
Afzaal Ahmad ZeeshanPosted Jan 25, 2016, 6:15 AM EST
var a=a+"abc" will be an error. Will show cannot convert type string to int. But b=b+"abc" will give you an output 5abc.
Aneez APosted Jan 22, 2016, 9:50 PM EST
it may return, 5+abc.
Barkha GuptaPosted Jan 22, 2016, 6:46 AM EST
var type of variables are required to be initialized at the time of declaration otherwise they would encounter the compile time error. However, for dynamic type, it would produce 5abc.
Anil Kumar MurmuPosted Jan 20, 2016, 10:28 PM EST
var a = 5;
a = a+"abc";
This code return compile time error.
dynamic b = 5;
b = b+"abc";
This code return 5abc..
Pramod ThakurPosted Jan 16, 2016, 2:52 AM EST

















